This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: The strange speed of a program!
- From: jlh <jlh at gmx dot ch>
- To: bear <xiongguanglei at tsinghua dot org dot cn>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 08 Jun 2005 15:26:41 +0200
- Subject: Re: The strange speed of a program!
- References: <318217302.11870@tsinghua.org.cn>
> I have a program whose speed is so strange to me. It is maily used
Same here (compiled with -O2):
./a.out 50 1024 0 -> CPU time: 6.510000 sec
./a.out 50 1024 1 -> CPU time: 3.880000 sec
but with different arguments:
./a.out 1000 128 0 -> CPU time: 0.270000 sec
./a.out 1000 128 1 -> CPU time: 2.960000 sec
This is also very interesting: If you copy the initializing
for-loop once *before* starting the timer, you can't reproduce
the odd effect anymore.
./a.out 50 1024 0 -> CPU time: 2.210000 sec
./a.out 50 1024 1 -> CPU time: 4.040000 sec
and
./a.out 1000 128 0 -> CPU time: 0.710000 sec
./a.out 1000 128 1 -> CPU time: 2.990000 sec
Someone more knowledgable than me will explain it using the
right terms. I can only say that it has something to do with
how the kernel (linux here) manages memory. I once found out
that memory physically gets allocated only at its first use,
not during the malloc(), i.e. very large chunks can be allocated
without causing any swapping. The swapping only starts to take
place when you use that memory for the first time. Maybe in
above case the kernel 'knows' that the memory pages are being
read without ever having been initialized and doesn't bother
loading anything... just speculation.
jlh