R
Rahul Gandhi
Which one is more fast?
malloc followed by memset
or
calloc
malloc followed by memset
or
calloc
Which one is more fast?
malloc followed by memset
or
calloc
Rahul Gandhi said:Which one is more fast?
malloc followed by memset
or
calloc
Which one is more fast?
malloc followed by memset
or
calloc
Which one is more fast?
malloc followed by memset
or
calloc
Rob said:(e-mail address removed) (Rahul Gandhi) wrote
It is platform dependent.
But do you really care about the speed of malloc? If you do you
have a part of your program that's not written very well.
You will also find that it's quite common for 'free' to be by far
the slowest.
Red functions tend to go faster. But you'll have to do some profiling to
determine the appropriate chromodynamic hue of each function.
In said:Which one is more fast?
malloc followed by memset
or
calloc
CBFalconer said:That will normally be because the system is searching for adjacent
areas to combine, which may well be O(n) where n is the count of
allocations and free blocks. This makes freeing a large
collection of items O(sqr(N)).
It is shown up quite dramatically
in the test suite for hashlib, which in turn was the inspiration
for nmalloc for the DJGPP system, where those two operations
become O(1) and O(n) respectively. Both can be found at:
<http://cbfalconer.home.att.net/download/>
(nmalloc is freely available under the DJGPP license. Hashlib is
under GPL).
I would suggest malloc() followed by memset() for all occasions.Dan Pop said:However, this should NOT be the criterion for choosing one or
another.
If your application needs zeroed memory, call calloc, otherwise call
malloc.
But the blue ones are more critical, because they're coming toward
you.
"Malcolm said:I would suggest malloc() followed by memset() for all occasions.
calloc() is a trap for the unwary, since floating point types and pointers
have all bits zero for 0.0 and NULL on almost all platforms, but this isn't
guaranteed.
Christian Bau said:Excuse me, but malloc () + memset () has exactly the same problem!
Keith Thompson said:I *think* Malcom's point is that memset() makes it more explicit that
the memory is being set to all-bits-zero, whereas calloc(), being more
implicit might lead the unwary to assume that floating-point values
are set to 0.0 and pointers to NULL.
I'm not convinced, either that Malcolm is right or that I'm correctly
guessing what he meant.
In said:I *think* Malcom's point is that memset() makes it more explicit that
the memory is being set to all-bits-zero, whereas calloc(), being more
implicit might lead the unwary to assume that floating-point values
are set to 0.0 and pointers to NULL.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.