Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About time spent have big difference in two running

Reply
Thread Tools

About time spent have big difference in two running

 
 
xianwei
Guest
Posts: n/a
 
      09-25-2007
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main ( int argc, char *argv[] )
{
long i = 10000000L;
clock_t start, end;
double duration;

printf("Time do %ld loop spent ", i);
start = clock();
while (i--);
end = clock();

duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);

return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

I run the above the program,
The first time I spent 0.031000 seconds.
The second time I spent 0.015000 seconds
If I try again and again, the time spent on will 0.031 or 0.015
seconds
Why have such big difference?

thank you!!!

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-25-2007
xianwei wrote:
>

.... snip ...
>
> I run the above the program,
> The first time I spent 0.031000 seconds.
> The second time I spent 0.015000 seconds
> If I try again and again, the time spent on will 0.031 or 0.015
> seconds. Why have such big difference?


Because the resolution of your clock is obviously roughly 0.0155 S.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-25-2007
xianwei wrote:
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int
> main ( int argc, char *argv[] )
> {
> long i = 10000000L;
> clock_t start, end;
> double duration;
>
> printf("Time do %ld loop spent ", i);
> start = clock();
> while (i--);
> end = clock();
>
> duration = (double)(end - start) / CLOCKS_PER_SEC;
> printf("%f seconds\n", duration);
>
> return EXIT_SUCCESS;
> } /* ---------- end of function main ---------- */
>
> I run the above the program,
> The first time I spent 0.031000 seconds.
> The second time I spent 0.015000 seconds
> If I try again and again, the time spent on will 0.031 or 0.015
> seconds


Have you ever seen a hummingbird, and wondered
how fast its wings flutter? You might try to find
an answer by measuring the time for ten million beats.
So you set up your highly accurate wingbeat counter,
and then you start your timer: an hourglass ...

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-25-2007
"xianwei" <(E-Mail Removed)> a ¨¦crit dans le message de news:
(E-Mail Removed). com...
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int
> main ( int argc, char *argv[] )
> {
> long i = 10000000L;
> clock_t start, end;
> double duration;
>
> printf("Time do %ld loop spent ", i);
> start = clock();
> while (i--);
> end = clock();
>
> duration = (double)(end - start) / CLOCKS_PER_SEC;
> printf("%f seconds\n", duration);
>
> return EXIT_SUCCESS;
> } /* ---------- end of function main ---------- */
>
> I run the above the program,
> The first time I spent 0.031000 seconds.
> The second time I spent 0.015000 seconds
> If I try again and again, the time spent on will 0.031 or 0.015
> seconds
> Why have such big difference?


I suspect the clock() function on your system has a granularity of around 15
milliseconds. If this is the case, the clock() function will return the
same value for all calls during each 15 millisecond interval. Depending on
when exactly you start your measurements within that interval, a task
lasting cloless than 15ms can be "clocked" as lasting 0ms or 15ms.
Similarly, one that takes between 15 and 30 might be reposted as taking
exactly 15ms or exactly 30ms.

On top of this granularity issue, you should look into the clock() function.
Does it measure elapsed time? total processor time? processor time spent in
your program vs time spent in the system? or something else even... Your
"timings" will also be affected by other tasks the computer performs while
your program executes, and many other characteristics of you system (cache
memory, bus sharing with i/o devices, etc.)

For your peticular concern, I suggest you try and synchronize your timing
efforts with this small loop:

clock_t last, start;

last = start = clock();
while (start == last) {
start = clock();
}

You should try and measure longer computations, by repeating them in a loop
or increasing the constants.

You should consider using more accurate timing functions such as
non-standard gettimeofday in Linux.

You should repeat the tests many many times, and average the results,
discarding extreme values.

Effective code profiling is *very* difficult. Drawing conclusions or making
changes from profiling data is not easy either: what holds on one
architecture does not necessarily on another one, even just slightly
different. There is no definitive truth in this domain.

--
Chqrlie


 
Reply With Quote
 
xianwei
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 8:15 pm, Eric Sosman <(E-Mail Removed)> wrote:

> Have you ever seen a hummingbird, and wondered
> how fast its wings flutter? You might try to find
> an answer by measuring the time for ten million beats.


Thank you, you are right, I should replace one millions to ten
millions.
When I do that, the time keep in 0.285 - 0.231 seconds, I think this
is
well.

To test how fast humming wings flutter sound not like a good
idea !!



 
Reply With Quote
 
xianwei
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 8:18 pm, "Charlie Gordon" <(E-Mail Removed)> wrote:
> "xianwei" <(E-Mail Removed)> a ¨¦crit dans le message de news:
> (E-Mail Removed). com...
>
>
>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <time.h>

>
> > int
> > main ( int argc, char *argv[] )
> > {
> > long i = 10000000L;
> > clock_t start, end;
> > double duration;

>
> > printf("Time do %ld loop spent ", i);
> > start = clock();
> > while (i--);
> > end = clock();

>
> > duration = (double)(end - start) / CLOCKS_PER_SEC;
> > printf("%f seconds\n", duration);

>
> > return EXIT_SUCCESS;
> > } /* ---------- end of function main ---------- */

>
> > I run the above the program,
> > The first time I spent 0.031000 seconds.
> > The second time I spent 0.015000 seconds
> > If I try again and again, the time spent on will 0.031 or 0.015
> > seconds
> > Why have such big difference?

>
> I suspect the clock() function on your system has a granularity of around 15
> milliseconds. If this is the case, the clock() function will return the
> same value for all calls during each 15 millisecond interval. Depending on
> when exactly you start your measurements within that interval, a task
> lasting cloless than 15ms can be "clocked" as lasting 0ms or 15ms.
> Similarly, one that takes between 15 and 30 might be reposted as taking
> exactly 15ms or exactly 30ms.


Thank you for you explanation about the question.

> You should repeat the tests many many times, and average the results,
> discarding extreme values.


Yes, when I larger the loop times, the spent times differs in a small
point.
Thanks you enthusiasm.


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-25-2007
xianwei wrote On 09/25/07 08:42,:
> On Sep 25, 8:15 pm, Eric Sosman <(E-Mail Removed)> wrote:
>
>
>> Have you ever seen a hummingbird, and wondered
>>how fast its wings flutter? You might try to find
>>an answer by measuring the time for ten million beats.

>
>
> Thank you, you are right, I should replace one millions to ten
> millions.
> When I do that, the time keep in 0.285 - 0.231 seconds, I think this
> is
> well.
>
> To test how fast humming wings flutter sound not like a good
> idea !!


The point is that the "granularity" of your measuring
instrument influences how precisely you can measure. An
hourglass is fine for measuring durations on the order of,
well, hours, but is not well suited for measuring milliseconds.
There are ways to improve the measurement precision of a
"coarse" clock; one of them is to measure more repetitions
of the activity whose duration interests you.

--
(E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-25-2007
CBFalconer <(E-Mail Removed)> writes:
> xianwei wrote:
> ... snip ...
>>
>> I run the above the program,
>> The first time I spent 0.031000 seconds.
>> The second time I spent 0.015000 seconds
>> If I try again and again, the time spent on will 0.031 or 0.015
>> seconds. Why have such big difference?

>
> Because the resolution of your clock is obviously roughly 0.0155 S.


Most likely 1/60 second, but that's just a semi-educated guess.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-25-2007
Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:
>> xianwei wrote:
>>
>> ... snip ...
>>>
>>> I run the above the program,
>>> The first time I spent 0.031000 seconds.
>>> The second time I spent 0.015000 seconds
>>> If I try again and again, the time spent on will 0.031 or 0.015
>>> seconds. Why have such big difference?

>>
>> Because the resolution of your clock is obviously roughly 0.0155 S.

>
> Most likely 1/60 second, but that's just a semi-educated guess.


PCs can have some peculiar number, tied back to the old XT.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      09-26-2007
On 2007-09-25 21:40, CBFalconer <(E-Mail Removed)> wrote:
> Keith Thompson wrote:
>> CBFalconer <(E-Mail Removed)> writes:
>>> Because the resolution of your clock is obviously roughly 0.0155 S.

>>
>> Most likely 1/60 second, but that's just a semi-educated guess.

>
> PCs can have some peculiar number, tied back to the old XT.


That would be 1/18.2 seconds (or rather 1 / (4.77E6 / 4 / 65536)
seconds). Was CLOCKS_PER_SEC actually a floating point constant on
MS-DOS compilers? I don't remember but I guess it must have been.

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Monitoring time spent running classes and methods Mario Ruiz Ruby 4 02-24-2009 12:57 PM
Time spent reading a file. ASP.Net (V1.1) v.anupama@gmail.com ASP .Net 5 04-26-2006 05:51 AM
profiling result panic: javax.swing.SystemEventQueueUtilities.queueComponentWorkRequest 56% of time spent? Dimitri Ognibene Java 7 04-25-2006 07:51 PM
Analyse time spent in synchronized blocks Dan Java 0 05-10-2005 10:16 AM
im new here and have spent hours trying to find more about spyware/addware.... drackon Computer Support 3 08-22-2003 07:17 PM



Advertisments