measure time eclapsed for a simple program

B

bluekite2000

I ran the following code for a square matrix whose size ranges from 1x1
to 1000x1000

start=clock();
//sequential access time measurement
for (i=0;i<Msize;i++)
for(j=0;j<Nsize;j++)
{
Matrixj]=1;

}
finish=clock();
duration=(double)(finish-start);

The duration is all 0 tick from matrix 1x1 to 500x500 but then it jumps
suddenly to 10000 for matrix 501x501. I have 2 questions:
1. why the sudden jump?
2. How do I get some number more accurate than 0 tick?
Ps CLOCKS_PER_SEC=1000000 for my computer
 
G

Gordon Burditt

start=clock();
//sequential access time measurement
for (i=0;i<Msize;i++)
for(j=0;j<Nsize;j++)
{
Matrixj]=1;

}
finish=clock();
duration=(double)(finish-start);

The duration is all 0 tick from matrix 1x1 to 500x500 but then it jumps
suddenly to 10000 for matrix 501x501. I have 2 questions:
1. why the sudden jump?


Just because CLOCKS_PER_SEC says the return value of clock() is in
microseconds doesn't mean the timer being used can time intervals
that short. Even systems using a sundial or something that counts
rings in trees for a clock can use CLOCKS_PER_SEC = 1000000.

Try looping and printing out the return value of clock() every time
it's different from the previous return value of clock().
My guess is you get: 0, 10000, 20000, 30000, 40000, ...

2. How do I get some number more accurate than 0 tick?

Run something that takes more time. Maybe this means slapping a
million-iteration loop around your code.
Ps CLOCKS_PER_SEC=1000000 for my computer

Gordon L. Burditt
 
S

SM Ryan

# The duration is all 0 tick from matrix 1x1 to 500x500 but then it jumps
# suddenly to 10000 for matrix 501x501. I have 2 questions:
# 1. why the sudden jump?

If you have caches and/or virtual memory, you should expect nonlinear
responses as the problem size increases. It's a feature, not a bug, of
pretending you have more memory than you really do.

# 2. How do I get some number more accurate than 0 tick?
# Ps CLOCKS_PER_SEC=1000000 for my computer

A microsecond is a long time if everything can be kept in cache. Also even though
the ticks may be microseconds, it doesn't guarentee the clock is that accurate.

Your system might have additional profiling calls that might allow you track things
like page miss, cache reload, high resolution cpu clock, high resolution wall clock,
etc.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top