D
dgoodmaniii
+AMDG
I have a question about execution speed between allocating
storage dynamically with malloc() and doing so statically
with arrays. Which is faster?
I've finished with K&R's Exercise 5-7, which asks to rewrite
a program sorting character arrays with qsort to use an
array for storage rather than calling alloc() (I used
malloc() from stdlib, just for the exercise), and asks, "How
much faster is the program?" I inferred from this that the
array version should be faster than malloc(). My admittedly
novice intuition leads me to the same conclusion.
However, when running the version with malloc() through
standard Linux time (on Debian GNU/Linux, kernel 2.6.26, on
an x86_64 processor), I got the following results for the
malloc() version:
*******
real: 0m0.029s
user: 0m0.024s
sys: 0m0.004s
*******
And the following results with the array version:
*******
real: 0m0.043s
user: 0m0.020s
sys: 0m0.020s
*******
In other words, the malloc() version ran faster. Both were
run on the same file, of course, which was of almost the
maximum size permitted by the program (4999 lines).
I'm not going to post the programs themselves, unless you
all tell me it's necessary to answer the question. But is
this the behavior one would expect?
I have a question about execution speed between allocating
storage dynamically with malloc() and doing so statically
with arrays. Which is faster?
I've finished with K&R's Exercise 5-7, which asks to rewrite
a program sorting character arrays with qsort to use an
array for storage rather than calling alloc() (I used
malloc() from stdlib, just for the exercise), and asks, "How
much faster is the program?" I inferred from this that the
array version should be faster than malloc(). My admittedly
novice intuition leads me to the same conclusion.
However, when running the version with malloc() through
standard Linux time (on Debian GNU/Linux, kernel 2.6.26, on
an x86_64 processor), I got the following results for the
malloc() version:
*******
real: 0m0.029s
user: 0m0.024s
sys: 0m0.004s
*******
And the following results with the array version:
*******
real: 0m0.043s
user: 0m0.020s
sys: 0m0.020s
*******
In other words, the malloc() version ran faster. Both were
run on the same file, of course, which was of almost the
maximum size permitted by the program (4999 lines).
I'm not going to post the programs themselves, unless you
all tell me it's necessary to answer the question. But is
this the behavior one would expect?