malloc timings

This is a discussion on malloc timings within the C Programming forums, part of the General Programming Boards category; Hi In the program I'm writing, I have the option of either calling malloc three times to assign memory X ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    malloc timings

    Hi
    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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

  8. #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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21