![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 4
| malloc timings In the program I'm writing, I have the option of either calling malloc three times to assign memory X to each, or calling malloc once by assigning memory 3*X, and do it by assigning a pointer to a pointer. \ I was trying to find what was the time dependence of these processes, or if it mattered at all. Do you have any pointers? Thanks |
| roboa1983 is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| I'd suggest you go for what makes the code easiest to read in the first instance. Yes there will be a time difference, but unless you're doing this 1000's of times in a loop, it's going to be hard to measure.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 4
| Thanks Salem I had gone with the easist to read part, but was worried there was a significance difference. |
| roboa1983 is offline | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| The typical rule is to not over-optimize. Make your program as easy to guess and read as possible. If you find it slow, then use a profiler to find the bottlenecks and then optimize them. Good to remember.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #5 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| Well, I don't know what you mean exactly. But keep in mind that calling multiple malloc() isn't a matter about timing. It is most important a matter of allocating continuous space. If you call malloc() three times you won't get a continuous space which may case programs to run slower (once I had that issue in a project). If you call once malloc() with a 3*X as you said then you get a continuous space of memory, which will have benefits. But depends of what you want to do |
| C_ntua is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| It's easier to call malloc 3 times, and unless you really need the speed of calling it once (3X memory), then you should go with the former. If you find out later that it's eating too much time, THEN go back and change it.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #7 |
| Technical Lead Join Date: Aug 2007 Location: London, UK
Posts: 723
| If your system supports it, you can use gethrtime() to time function calls. Just remember that running the code once one way and once another will not give you an accurate timing comparison. You'll need to run it a couple of hundred times to get average timings. QuantumPete
__________________ "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support "Have you tried turning it off and on again?" - The IT Crowd |
| QuantumPete is offline | |
| | #8 |
| Registered User Join Date: Jun 2008
Posts: 4
| Thanks for the replies. I'm doing some timings with the original call (calling malloc three times), since it's either to understand. I'll look at the other technique later on, should other problems arise. |
| roboa1983 is offline | |
| | #9 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| First of all malloc is not a function that takes the same time each time - sometimes it takes longer than other times. How much, and how often depends on the individual implementation of malloc. But for the general rule and averaged over many calls, it's not an overly long function. Unless you are mallocing really small blocks of memory, it's probably many times longer to fill the data into the allocated block than the overhead of the malloc call. Malloc is, generally speaking, a fairly well optimized function, since it is use by most applications, and at least some of that use would be in performance critical code. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
![]() |
| Tags |
| malloc |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| malloc + segmentation fault | ch4 | C Programming | 5 | 04-07-2009 03:46 PM |
| the basics of malloc | nakedBallerina | C Programming | 21 | 05-20-2008 02:32 AM |
| Is there a limit on the number of malloc calls ? | krissy | Windows Programming | 3 | 03-19-2006 12:26 PM |
| Malloc and calloc problem!! | xxhimanshu | C Programming | 19 | 08-10-2005 05:37 AM |
| malloc() & address allocation | santechz | C Programming | 6 | 03-21-2005 09:08 AM |