Article View: rec.games.programmer
Article #2029Re: cout vs printf speed (was: Should I learn C or C++ first?)
From: lemahieu
Date: Thu, 31 Aug 1995 17:08
Date: Thu, 31 Aug 1995 17:08
36 lines
2358 bytes
2358 bytes
Gavin Estey <gavin@senator.demon.co.uk> wrote: >In article <809779542snz@onex.demon.co.uk> > Martin@onex.demon.co.uk "Martin Randall" writes: > >> >> OK, here's a new one to throw into this thread - but not totally un-related. >> Which is faster new or malloc ? My C++ book says that new should be faster. > >Why should you care which one is faster? You don't usually allocate memory >in inner loops or speed critical areas. New is better, maybe not faster, >but better. > >-- >Gavin Estey Why should anyone care which is faster?!? I can think of many applications where the speed of dynamically allocating memory is key to performance. For example, take any communications layer that is transferring packets of data around. Often receipt of a message would require a malloc and a memcpy. For packet sizes of less than 4Kbytes, the numbers on my platform show that the cost of a malloc is more than that of a memcpy. For very small messages (on the order of tens of bytes) the cost is basically entirely a malloc. At this point the performance of malloc is very important, and you have to decide whether to manage your own memory or not. I would also think that anyone writing code that does heavy manipulation of dynamic data structures (requiring lots of news and deletes as elements are added and removed) would be concerned about the cost of memory allocation. Applications like this further highlight the difference between new and malloc since new is invoking the constructor each time, which may make the allocation of small objects relatively expensive. Stroustrup addresses the small object allocation problem in his book on pages 176-178. --Paul
Technical Details
Message-ID:
<424qas$a1d@gap.cco.caltech.edu>Path:
arc1-ca.pugleaf.net!nntp-server.caltech.edu!newsReferences:
<809779542snz@onex.demon.co.uk> <809810607snz@senator.demon.co.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!nntp-server.caltech.edu!news From: <lemahieu@paradise.caltech.edu> Newsgroups: rec.games.programmer,comp.lang.c++ Subject: Re: cout vs printf speed (was: Should I learn C or C++ first?) Date: 31 Aug 1995 17:08:44 GMT Organization: California Institute of Technology, Pasadena Message-ID: <424qas$a1d@gap.cco.caltech.edu> References: <809779542snz@onex.demon.co.uk> <809810607snz@senator.demon.co.uk> NNTP-Posting-Host: dali.paradise.caltech.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.1N (X11; I; AIX 2) X-URL: news:809810607snz@senator.demon.co.uk