News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Any good code timers out there?

Started by OceanJeff32, February 15, 2005, 01:44:40 AM

Previous topic - Next topic

OceanJeff32

Mark, I was just reading your optimization page, and thought I would ask everyone (or you especially) if you had any good code timers that would work, I've tried messing around with RDSTC, but then I had a problem outputting results.

Later,

Jeff C
:U
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

Mark_Larson



  Yes.  MichaelW posted some sample code a while back.  There was a slight bug in the timing code, so I am not sure if he fixed it or not. 

http://www.masmforum.com/simple/index.php?topic=359.0


  MichaelW, did you fix the bug in the timing macro messages?  We need to make it a sticky.  It's important.


BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm

OceanJeff32

That's great, but it counts the processor cycle time, I was thinking of a timing loop in code to test snippets like:

save info

run code to time

restore info and calculate time code took to process.

I've done some work with RDTSC, but the results are hard to understand, I'm working on a timer for my code right now.

I'll post it when it's done.

Later,

Jeff C
:green2
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

Randall Hyde

Quote from: OceanJeff32 on February 15, 2005, 02:56:43 AM
That's great, but it counts the processor cycle time, I was thinking of a timing loop in code to test snippets like:

save info

run code to time

restore info and calculate time code took to process.

I've done some work with RDTSC, but the results are hard to understand, I'm working on a timer for my code right now.

I'll post it when it's done.

Later,

Jeff C
:green2

Here is the code I use to time the compilation time of an HLA v2.0 program:


static
    // Timer, used to time compiles

    align(4);
    compileTime     :real64;
    compileTimeInt  :uns32;
    compileTimer    :timer;

    timeStamp       :dword[2];
    timerTemp       :dword[2];
    ClocksPerMS     :uns64;
    CompilationTime :uns64;
    longLineCnt     :uns64;
    linesPerSecond  :uns64;
    MHz             :uns32;

  .
  .
  .
    compileTimer.create();
  .
  .
  .
        if( VerboseCompile ) then

            compileTimer.start();

            // Wait for the timer to change:

            compileTimer.checkPoint();
            mov( eax, timerTemp );
            while( eax = timerTemp[0] ) do

                compileTimer.checkPoint();

            endwhile;
            mov( eax, timerTemp );
            mov( edx, timerTemp[4] );
           
            // Okay, now compute the number of clock cycles between
            // timer changes:

            rdtsc();
            mov( eax, timeStamp );
            mov( edx, timeStamp[4] );
            repeat

                compileTimer.checkPoint();

            until( eax <> timerTemp[0] );
            sub( timerTemp, eax );
            sbb( timerTemp[4], ecx );
            mov( eax, timerTemp );
            mov( edx, timerTemp[4] );

            rdtsc();
            sub( timeStamp, eax );
            mov( eax, timeStamp );
            sbb( timeStamp[4], edx );
            mov( edx, timeStamp[4] );

            // Compute the number of clock cycles per millisecond

            math.divq
            (
                (type qword timeStamp),
                (type qword timerTemp),
                ClocksPerMS
            );

            // We're done with the timer.

            compileTimer.stop();

            // Use the "read timestamp counter" instruction to actually
            // time the compilation:

            rdtsc();
            mov( eax, timeStamp[0] );
            mov( edx, timeStamp[4] );

        endif;
  .
  .
  .
        if( VerboseCompile && totalLineCount <> 0 && !testMode ) then

            rdtsc();
            sub( timeStamp[0], eax );
            sbb( timeStamp[4], edx );
            mov( eax, timeStamp[0] );
            mov( edx, timeStamp[4] );

            fild( ClocksPerMS );
            fld( 1000.00 );
            fdiv();
            fistp( MHz );
            mov( MHz, eax );
            xor( edx, edx );
            idiv( 10, edx:eax );
            imul( 10, eax );
            mov( eax, MHz );
            if( eax > 1000 ) then

                xor( edx, edx );
                idiv( 1000, edx:eax );
                stdout.put
                (
                    nl
                    "Approximate computer speed: ",
                    (type uns32 eax ),
                    '.'
                );
                mov( edx, eax );
                xor( edx, edx );
                idiv( 10, edx:eax );
                stdout.putu32Size( eax, 2, '0' );
                stdout.put( " GHz" nl );               

            else

                stdout.put( "Approximate computer speed: ", MHz, " MHz" nl );

            endif;
            stdout.put
            (
                "Total number of lines compiled: ",
                totalLineCount,
                nl
            );


            // Compute the compile time in milliseconds and print
            // the compile time:

            math.divq( (type qword timeStamp), ClocksPerMS, CompilationTime );
            stdout.put( "Compile time: " );
            if( (type dword CompilationTime) = 0 ) then

                // If the compile time was less than one millisecond,
                // then print the number of microseconds:

                math.mulq( (type qword timeStamp), 1000, CompilationTime );
                math.divq
                (
                    CompilationTime,
                    ClocksPerMS,
                    CompilationTime
                );
                stdout.put
                (
                    (type uns32 CompilationTime),
                    " us"
                    nl
                );

           
            elseif( (type dword CompilationTime) < 1000 ) then

                // If the compilation time is between 1 and 1,000 ms,
                // then print the time in milliseconds:

                stdout.put
                (
                    (type uns32 CompilationTime),
                    " ms"
                    nl
                );

            else

                // If the compilation time is greater than 1,000 ms, then
                // print the compilation time in seconds (fractional, to
                // three decimal places).

                mov( (type dword CompilationTime), eax );
                mov( (type dword CompilationTime[4]), edx );
                div( 1000, edx:eax );
                stdout.putu32( eax );
                stdout.putc( '.' );
                stdout.putu32Size( edx, 3, '0' );
                stdout.put( " ms" nl );

            endif;

            // Compute and print the compile rate in lines/sec:

            mov( totalLineCount, eax );
            mov( eax, (type dword longLineCnt ));
            mov( 0, (type dword longLineCnt[4]));
            math.mulq( ClocksPerMS, longLineCnt, longLineCnt );
            math.mulq( 1000, longLineCnt, longLineCnt );
            math.divq( longLineCnt, timeStamp, linesPerSecond );
            stdout.put( "Compile rate: ", linesPerSecond, " lines/sec" nl );

        endif;


This code snippet uses the HLA Standard Library timer class (among several other HLA stdlib routines). For more details on the HLA timer class, see
http://webster.cs.ucr.edu/AsmTools/HLA/HLADoc/HLAstdlib/hlastdlib30.html#1028768
It basically measures the time, in milliseconds, between two events. Alas, the granularity of the Windows timer appears to be 10 ms, so you can't use this facility by itself to measure typical code sequences. I use the timer to determine the processor's clock frequency and then use the RDTSC values to compute the actual time of the compilation (that is, the cost, in cycles, of the call to the compile function in HLA v2.0).  Quite a bit of code, but quite accurate for relatively long code sequences (ignoring, of course, interrupts and multitasking which can mess up critical measurements that take some time).
Cheers,
Randy Hyde

OceanJeff32

I read all the HLA documentation a while ago, it was what got me started in Assembly Language recently.  Thank you, by the way.

I tried and ran a few simple programs. 

But I wanted support for graphics, and at the time I was all about Direct X.  So I switched back to C++.  Made my application in Direct X 9.0 (whatever edition they had at the time).

I got a hold of Michael Abrash's Zen of Graphics Programming, and Zen of Assembly.  But those required 16-bit assembler.  Ended up using DEBUG to make .com files and programmed for VGA, etc.  It was a big hairy mess, in the middle of that, I read the Master Book of Assembly Programming, which was all about MASM and Windows.  And I ran across the Fireworks code on www.ronybc.8k.com, and those two things convinced me to pursue MASM again (and Windows GDI, scary enough as that is...)

I guess it's been about a year since I've looked at HLA, I should re-consider it again, it was an awesome program, and the documentation was excellent.

You're books are too expensive though...  :toothy

I've been pricing all of your books online since last year!

HLA uses MASM in the background doesn't it, the HLA makes a MASM compatible ASM file.  I'll have to see if you have support for MMX, and SSE2, and I know you just wrote (or are still writing) that whole book on Windows (god help you...LOL)  :bg

Later,

Jeff C
:U

P.S. Nice to chat with you, btw, I saw that spot you did on TV! Pretty cool!
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

Vortex


Randall Hyde

Quote from: OceanJeff32 on February 18, 2005, 01:47:32 AM
I guess it's been about a year since I've looked at HLA, I should re-consider it again, it was an awesome program, and the documentation was excellent.

You're books are too expensive though...  :toothy
??? HLA documentation and the art of assembly are available free on the internet.
http://webster.cs.ucr.edu. You *can* buy a published edition of AoA, but there is still an electronic version available for free on the net.

Quote
I've been pricing all of your books online since last year!
http://www.artofasm.com
Of course, if you decide to print the whole thing out, it will probably cost you as much in paper and inkjet cartridges as the published edition  :bdg TINSTAAFL.

Quote
HLA uses MASM in the background doesn't it, the HLA makes a MASM compatible ASM file. 
HLA can compile to MASM, TASM, Gas, or FASM at this point. Though such intermediate files are generally of little interest to most HLA users. You rarely consider such files. The HLA compiler automatically runs the whole process of converting HLA source code to an executable file (running the HLA parser, the secondary assembler, the linker, and other necessary tools). Other than the fact that a few extra files appear on your disk after the compilation, the whole process is transparent to the HLA user.


Quote
I'll have to see if you have support for MMX, and SSE2, and I know you just wrote (or are still writing) that whole book on Windows (god help you...LOL)  :bg

Later,

Jeff C
:U

P.S. Nice to chat with you, btw, I saw that spot you did on TV! Pretty cool!

HLA certainly supports MMX and SSE2. However, to use SSE2 you need a secondary assembler that supports those instructions. The typical MASM programs you'll find lying around don't support these extended instructions and I *have* encountered some problems using HLA with MASM v7 (which supports them all). I recommend using FASM as the intermediate assembler if you want to use SSE2 instructions.
Cheers,
Randy Hyde

daydreamer

I really want a link to that tv interview with Randy

OceanJeff32

Yes, at the time, that's what I did.  I wasn't into buying books at the time, so I scoured the internet looking for free .pdf files.

I think I've read your whole book, at one time, I was all about HLA. (I printed it out, one chapter at a time and then took the chapters to work daily.)

daydreamer: I think the link is on webster  (randy's address at webster)

Later,

Jeff C
:green
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

Randall Hyde

Quote from: daydreamer on February 18, 2005, 08:08:17 PM
I really want a link to that tv interview with Randy


Last time I checked, they'd removed the segment from the web page to make room for other shows. Of course, who knows what Tech TV is doing these days (since they were bought out).
Cheers,
Randy Hyde