TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Darin McBride
from: Bill Birrell
date: 2004-04-14 16:51:00
subject: Squares

Hi Darin,

 > Well, that can be somewhat painful.  Depending on your
 > skill with assembly, you may want to compile both
 > programs and get the assembler for it, and look at
 > that, it may show you what instructions are used.
 > While this isn't exactly a 100% fool-proof method,
 > either (different instructions take different numbers
 > of clock cycles), it's usually reasonably safe to
 > assume that more instructions take longer than fewer
 > instructions, especially when you're comparing hand
 > optimised vs non-optimised code that is intended to do
 > the same thing.

    That is one way, sure. On the other hand you could just use the elapsed
time utility to run each of the programs, and let the machine do the
tedious bit for you. After all it's why we bought the machines. :-)

    Conceptually it is easy - just spawn or fork the program to be timed,
and do a clock() when it returns from the spawned program, and Bob's your
uncle - elapsed time in milliseconds (on this machine) or whatever. As long
as it is only comparing one with the other, it is not necessary to demand
perfect accuracy. Assume that once compiled spawnl() will take roughly the
same amount of time for each instance.

#include 
#include 
#include 
int main(int argc, char *argv[])
{
        clock_t elapsed_time;
        spawnl(P_WAIT,argv[1],argv[1],NULL);
        elapsed_time = clock();
        printf("\nElapsed time is %ld ", elapsed_time);
        return 0;
}

 > I would always suggest that if you're dealing with a
 > constant, mark it as such.  Don't allow some poor
 > compiler coder get it wrong.  Oh, and better yet,
 > don't allow the reader of your code to get confused
 > ;-)

    Yup. That was precisely the point of doing it manually. It would take a
great dunce to misunderstand the purpose of the top identifier in the
snippet I quoted.

Best Wishes,
Bill.

---
* Origin: Escan BBS (2:25/200)
SEEN-BY: 633/267 270
@PATH: 25/200 108 252/110 250/501 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.