how can we calculate the time spent on executing a particular set of statements

Recommended Answers

All 8 Replies

time.h. Look at the example on difftime().

Using time.h to time code is like using a sundial to time a bullet.

You could use it if you're doing a lot of work (say sorting 1M integers), then time.h might work (so long as it takes several seconds).

But for anything consistently below 1 second, you need a much faster clock. However, pk09 needs to say which OS/Compiler is being used here, because there is nothing standard about how to access faster clocks.

i am using windows vista and the compiler is borland turbo c++4.5

Well that's one way to screw things up I suppose.

You're stuck in a 16-bit emulation of an x86, unable to access the 32-bit goodness of your real OS or real processor.

Perhaps consider upgrading to a compiler which is actually compatible with your OS.
Then you could use QueryPerformanceCounter

As it is, you could try this

start = clock();
for ( i = 0 ; i < 1000 ; i++ ) {
  doCodeToBeTimed();
}
end = clock();

Divide the difference by 1000 to get an average per run.
Increase the number of iterations until you get an overall loop time which takes the order of seconds to complete.

sum()
integer x,y,z
read x,y
z=x+y
print"the sum of x and y is",z
end sum()

sum()
integer x,y,z
read x,y
z=x+y
print"the sum of x and y is",z
end sum()

In the relatively short time I've been participating in this forum, this would have to be one of the most irrelevant answers to a thread I have seen.

keerthiga, did you actually read the OP's original query? What a silly little answer.

i'm really interested in the question posted to us by our co-member. I really read carefully each replies (including the quotes of course) until i reached keerthiga's post, that's really irrelevant.

Well, anyways, i like it. It's up to us to weigh things.

sum()
integer x,y,z
read x,y
z=x+y
print"the sum of x and y is",z
end sum()

Oh man, this is really funny. The OP is asking for elapsed time and you're telling him/her to add two integers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.