speed of execution

C

Chris Hills

Nelu said:
where CLOCKS_PER_SEC equals 1000000 this function will return the
same value approximately every 72 minutes".
If this is the case, I hope his program doesn't need 72 minutes to
complete.
In that case he's better off using a stop watch :).

I once had a customer call me to say that after the upgrade to the new
version of a compiler/simulator (from several versions older) the
simulator was complete CRAP!!!

Apparently.... and this is absolutely true, he had been timing programs
running in the simulator with a stopwatch!!! In the new simulator the
timing was way out.

It appeared that on the particular PC he used with that particular
version of the simulator for his particular application the simulator
was running in approximate real time. SO he could time it +/- a second
with a stopwatch.

He now expected me to fix the new version of the simulator so that on
his new PC and OS it ran in real time like the old one. If I couldn't as
far as he was concerned it was broken and he wanted his money back!!!

That is not something I saw on the net, or happened to a friend... It
happened to me and I had to convince this "engineer" that his new
simulator was not broken because it did not run in real time.
 
W

Walter Roberson

Many usenet readers use news readers
that are threaded - I've been reading usenet for 24 years and I've
never used one that wasn't.

Which threaded newsreaders did you use in 1982 (24 years ago)
and 1983 (23 years ago)? We should get the official histories
corrected, as they make the mistake of saying that the first
threaded newsreader was nn, written in 1984 (22 years ago).

http://en.wikipedia.org/wiki/Trn
 
W

Walter Roberson

unsigned long speed(unsigned long distance,
unsigned long time)
{
return distance / time;
}

That's not very robust: it truncates the results, and so has
an accuracy that depends crucially on the units.

It also fails to check whether time is 0 before doing the division;
the probability of time being 0 is going to depend upon the units
in the best of circumstances, and if the time is the difference between
clock() readings then that difference is going to be 0 on implementations
that do not have a realtime clock available.
 
N

Nelu

Chris said:
<snip>

Apparently.... and this is absolutely true, he had been timing programs
running in the simulator with a stopwatch!!! In the new simulator the
timing was way out.
<snip>

I had a coleague at the University like that. That's why I asked about
the multithreaded
environment. He was comparing MS DOS with other systems, but when he
was timing
the program under MS-DOS he was going for a smoke and when he was
timing
the one under Windows 95 he was playing some robots game that was using
enough CPU
to matter.
He was also using a stop watch :).
 
B

Bill Pursell

Richard said:
Ian Collins said:


That only tells you the time of execution. To calculate the speed, you will
also need to know the distance the program travels within that time.

This requires the existence of both time and space. I don't think
the standard requires either of these, so the entire idea of
judging the speed of a program is probably non-portable.
 
R

Richard Heathfield

Bill Pursell said:
This requires the existence of both time

4.12.2.4 The time function

Synopsis

#include <time.h>
time_t time(time_t *timer);

Description

The time function determines the current calendar time. The
encoding of the value is unspecified.

and space.

2.2.1 Character sets

[...] Both the basic source and basic execution character sets shall have
at least the following members: the 26 upper-case letters of the
English alphabet [elided] the 26 lower-case letters of the English alphabet
[elided] the 10 decimal digits [elided] the following 29 graphic characters
[elided] the space character,"
I don't think
the standard requires either of these,

I beg to differ. :)
 
K

Kevin Handy

Richard said:
Ian Collins said:




That only tells you the time of execution. To calculate the speed, you will
also need to know the distance the program travels within that time.

In these times, isn't it more important to know it's
fuel economy? i.e. distance / fuel used, preferably in
miles/gallon or kilometers/liter.
 
D

David Wade

The clock function in said:

Just an aside. In order to know the speed of code, you need to know what you
are executing. In the general case with "C" this is not possible as the
optimiser can reorder the code. I guess its should know about "clock()" but
as "start" and "stop" are local variables in the above code, I would assume
that some of the more basic optinizers could re-arrange the coide as :-

start =stop= clock();

/*
** Timed event gets moved here
*/

Because after all , if the code in timed event does not access any
externals, then it can't affect the results returned by clock....

Or have I just had too much "bombardier" preium bitter 5.2% abv.

Dave.



http://www.wpsoftware.net/blog >
 
T

Thad Smith

kavi said:
Could any one tell the way of calculating the speed of c program
execution?

In general, it is very difficult. I have done it in special cases, such
as timing loops for an 8-bit embedded processor. You need to know the
machine instructions generated by the compiler, the number of processor
cycles each instruction takes to execute, and the processor clock frequency.

For desktop machines (and other large processors) it is much more
difficult because of pipelined execution, cache hit/miss, branch
prediction, interrupt loading, and other factors that make the machines
fast, but their timing less predictable.

If you want to know how to _measure_ the speed of code running on a
particular machine with a particular environment, that is a very
different question.
 
M

Malcolm

kavi said:
Hello friends,

Could any one tell the way of calculating the speed of c program
execution?
Go into the inner loop of your program and count how many memory access it
makes, assuming it calls no library functions. Then add a bit.
 
P

pete

David Wade wrote:
Just an aside. In order to know the speed of code,
you need to know what you are executing.
In the general case with "C" this is not possible as the
optimiser can reorder the code.
I guess its should know about "clock()"

It's exactly the opposite.
The compiler doesn't know what any standard library functions do.
but as "start" and "stop" are local variables in the above code,

If they were external variables, then that would matter, how?
I would assume
that some of the more basic optinizers could re-arrange the coide as :-

start =stop= clock();

/*
** Timed event gets moved here
*/

Because after all , if the code in timed event does not access any
externals, then it can't affect the results returned by clock....

The compiler doesn't know what any functions do,
so it isn't free to replace two function calls with one.

x = getchar();
x = getchar();
Or have I just had too much "bombardier" preium bitter 5.2% abv.

By process of elimination, that's the problem.
 
B

Bill Pursell

Richard said:
Bill Pursell said:
Richard Heathfield wrote, regarding speed of a program:
[Timing it] only tells you the time of execution. To calculate the speed, you
will also need to know the distance the program travels within that time.

This requires the existence of both time

4.12.2.4 The time function

Synopsis

#include <time.h>
time_t time(time_t *timer);

Description

The time function determines the current calendar time. The
encoding of the value is unspecified.

and space.

2.2.1 Character sets

[...] Both the basic source and basic execution character sets shall have
at least the following members: the 26 upper-case letters of the
English alphabet [elided] the 26 lower-case letters of the English alphabet
[elided] the 10 decimal digits [elided] the following 29 graphic characters
[elided] the space character,"
I don't think
the standard requires either of these,

I beg to differ. :)

So if whitespace in the source is how space (and therefore
distance) is measured in a program, then increasing the
whitespace should make your code run faster since it
increases the distance without affecting the run time.
That certainly puts optimization in a different perspective. :)
 
K

Keith Thompson

pete said:
It's exactly the opposite.
The compiler doesn't know what any standard library functions do.

Actually, the compiler is allowed to use its knowledge of how standard
library functions behave, though it's not likely to be useful in the
case of clock().
If they were external variables, then that would matter, how?

It could matter in some cases, but not in this one. For example, if
they were local, then their values on leaving the block wouldn't
matter, which could introduce some opportunities for optimization.
The compiler doesn't know what any functions do,
so it isn't free to replace two function calls with one.

x = getchar();
x = getchar();

You're right in the case of clock() and getchar(), but not in general.
For example, given

y = sin(x);
y = sin(x);

the compiler is free to generate just one call to sin() (or none, if
it can determine the value of x at compile time, or if the result
isn't used).

I've seen this:
printf("hello, world\n");
compiled to the equivalent of this:
puts("hello, world");
 
P

pete

Bill said:
Richard said:
Bill Pursell said:
Richard Heathfield wrote, regarding speed of a program:

[Timing it] only tells you the time of execution.

That would be the appropriate definition of "speed"
to use in this case.
So if whitespace in the source is how space (and therefore
distance) is measured in a program, then increasing the
whitespace should make your code run faster since it
increases the distance without affecting the run time.
That certainly puts optimization in a different perspective. :)

If you want to continue pretending that the
definition of "speed"
in the context of high school physics class,
is the appropriate definition to use here,
then yes.

http://dictionary.reference.com/search?q=speed&db=*

1.Physics. The rate or a measure of the rate of motion, especially:
a.Distance traveled divided by the time of travel.

2.Swiftness of action.
 
R

Richard Harter

"Richard Harter" writes: [snip]
In short, your comment was an overstatement.

Thanks for clarifying that. I'll bet there is a lots more traffic now than
there was in 1982?

Likely enough. There has been an enormous expansion of usenet traffic
since then, but not so much in the C newsgroup. I opine that the
difference in traffic between net.lang.c in the 80's and comp.lang.c
now is about a factor of two or three.
Society depends on vigilant, public-spirited people like you to keep the
rest of us on the straight and narrow. Keep up the good work!

Thank you for your kind words and your thoughtful consideration. I
have every confidence that you will reform your ways.

Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
It is not wise to examine apparent coincidences too closely.
Sometimes they are not coincidences at all.
 
R

Richard Harter

Which threaded newsreaders did you use in 1982 (24 years ago)
and 1983 (23 years ago)? We should get the official histories
corrected, as they make the mistake of saying that the first
threaded newsreader was nn, written in 1984 (22 years ago).

http://en.wikipedia.org/wiki/Trn

I stand corrected. It's been a while, you know.




Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
It is not wise to examine apparent coincidences too closely.
Sometimes they are not coincidences at all.
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top