Questions on Speed of Execution

This is a discussion on Questions on Speed of Execution within the C Programming forums, part of the General Programming Boards category; I am very new to C but have written a program in C with quite a lot of inline assembly ...

  1. #1
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26

    Question Questions on Speed of Execution

    I am very new to C but have written a program in C with quite a lot of inline assembly code as speed is crucial. I am using QuickC version 2.51 which is a very old DOS based program but run in an easy to use visual interface. It produces DOS code which then runs in a DOS window.

    The innermost loop executes about 50 instructions on each pass of which 25 are C instructions and 25 are asm. On my 266Mhz Pentium running windows 98 I find that this loop runs at the rate of about 1M passes per sec. This corresponds nicely with my perception of C and asm speeds ( an asm "instruction" being about 10 times quicker than a C "instruction", say 300M per sec as opposed to 30M per sec for C - but I have reached the limit of replacing C code with asm ) When I run this code on my other machine which is a 800MHz Pentium under win2000 professional I find it runs at 3M per sec which is exactly in accord with my expectations ( the processor is three times quicker and the program runs three times quicker - no surprise so far )

    Now for the crunch. I recently tried the program on a new 1.8GHz machine running Windows XP and to my surprise it ran at 3M per sec! In other words no increase in speed. Now I appreciate it is easy to say that Win XP is slow but reviews suggest that it is based on win2000 anyway and is quicker for many aplications.

    Now the questions. My program is pure computation - there is no output to devices - it just sits and calculates for days at a time. Nor is there much RAM memory accessing. There is no disk accessing. The total program is about 1,300 lines long and compiles to a 70K exe file but I would not imagine that is particularly relevant. So

    Q1. Why is there no speed increase under win XP?

    Q2. Would compiling the program on a win based compiler make it run faster?

    Q3 Would it be quicker written in C++ or in a 32 bit word version of C or any other version of C?

    Q4 Any other comments or suggestions as to how to make it run faster on a newer machine? Or on an older machine for that matter!

    Many thanks in anticipation

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    In short order, I would re-write the code so that it was all C (no asm at all), and re-compile it with a 32 bit compiler - probably as a console application.

    You get the following advantages
    1. the code generated by the compiler will make use of all the pentium features - like 32 bit operations and instruction scheduling.

    2. compiler optimisers have come a long way since whatever you seem to be using was current.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    True, there is no DOS involved in Microsoft Professional OS's like Win2k or even XP Home. DOS is a virtual simulation that is nothing more than Win32. Your best bet would be to modernize with the OS you are using. The DOS virtual machine is built for backwards compatibility, not for performance.

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Looks like QuickC 2.51 is from the late 80's (copyright 1987-1989). If you believe this Microsoft researcher, then advances in compiler optimizations have almost doubled since then.

    Also, I'd be interested to hear if you notice a speed increase switching to a POSIX operating system running under similar hardware (if you get the chance).
    Jason Deckard

  5. #5
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26

    Question

    Many thanks for the answers. Before re-writing for a 32 bit windows compiler I would be very grateful if people could compile the test program below and see how long it takes on various systems. I'm sure this would be of interest to many people apart from me. My results are:

    Pentium 266MHz on win98 = 3.19 secs

    Pentium 800Mhz on win2000 prof = 0.99 secs

    Code:
    #include<stdio.h>
    #include<math.h>
    #include <time.h>
    
    #define LOOPS (10000000)
    
    main(){
      long j;
      float a=1000,temp;
      time_t cstart, cend;
    
      cstart=clock();
      system("cls");
      while(j<LOOPS){
        j++;
        switch(j%8){
          case 0:
    	a=a*1.001;
    	break;
          case 1:
    	a=a/1.001;
    	break;
          case 2:
    	a=a*1.001;
    	break;
          case 3:
    	a=a/1.001;
    	break;
          case 4:
           a=a*1.001;
           break;
          case 5:
    	a=a/1.001;
    	break;
          case 6:
    	a=a*1.001;
    	break;
          case 7:
    	a=a/1.001;
    	break;
        }
      }
      cend=clock();
      if(cend!=cstart){
        temp=((double)cend-cstart)/CLK_TCK;
        printf( "Program took %-5.2f secs for %-lu loops",temp,LOOPS);
      }
      while(!kbhit());    //Loop until any key hit ( to stop window closing )
    }

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Q1. Why is there no speed increase under win XP?

    Speed is mainly dependent on the machine you're using.

    >Q2. Would compiling the program on a win based compiler make >it run faster?

    As I understood from Deckard, you're using a quite old compiler. Yes, a new compiler would surely help to make things faster.

    >Q3 Would it be quicker written in C++ or in a 32 bit word >version of C or any other version of C?

    There are no different versions of C. Though there are different versions of compilers. You should look for a compiler which fully uses all advanced features of your microprocessor.

    >Q4 Any other comments or suggestions as to how to make it >run faster on a newer machine? Or on an older machine for that >matter!

    When putting the code on to a different machine, you should take a look at your assembly code, there may be things which you could change to increase speed.. The newer machine may have new features which increases the speed of your software.

  7. #7
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26
    Thanks Shiro. My question re XP was badly worded but was ( maybe ) clear from what went before. It should have read:

    Q1. Why is there no speed increase when I run the program on a machine which has a clock speed of 1.8GHz running win XP as opposed to a machine with clock speed of 800MHz running win2000 prof?

    As for "different versions of C". Yup! Guilty as charged. What I meant was "Is there some magical way to make this program run ten times faster ..."

    Concerning the assmbly code it already runs at one instruction per clock tick ( eg about 300M per sec on the 266Mhz machine ) Can it exceed the clock speed? Caches, pipelines etc?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well after I'd fixed the errors in the test program, I got these results (P2-233, Win95, DJGPP 2.8.1)

    > long j;
    Uninitialised var - pretty important you really know how many times you loop if you intend to time it.

    gcc cpr-test.c
    a.exe
    Program took 2.14 secs for 10000000 loops
    a.exe
    Program took 2.09 secs for 10000000 loops
    a.exe
    Program took 2.03 secs for 10000000 loops

    And with the optimiser turned on
    gcc -O2 cpr-test.c
    a.exe
    Program took 0.44 secs for 10000000 loops
    a.exe
    Program took 0.49 secs for 10000000 loops
    a.exe
    Program took 0.44 secs for 10000000 loops

  9. #9
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26
    Many thanks Salem - especially for pointing out my crass error in not setting j at zero. First part of code should read:

    long j = 0;

    My revised timings using QuickC 2.51 after correcting the error are:

    Pentium 266MHz on win98 = 4.12 secs

    Pentium 800Mhz on win2000 prof = 0.99 secs

    Concerning your timings I deduce after a quick search on the web that the P2-233 is a 233Mhz machine and that DJGPP ( forgive my ignorance ) is " ... a compiler and a set of tools that let you produce 32-bit protected-mode programs which run on MS-DOS/MS-Windows machines"

    I have to say I am astonished at the difference in your speeds when the optimiser is turned on - 0.44 secs seems incredibly fast. I will have to investigate DJGPP ... here is some stuff on it for those as ignorant as me:

    http://www.delorie.com/djgpp/v2faq/faq2.html

    Many thanks. One of the greatest difficulties of the solo hobby programmer is getting this kind of feedback.

    Finally, do I take it that DJGPP is as fast as it gets or are there even faster options?

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Pentium III - 800 Mhz, Win2k,


    MSVC++ 6.0 Enterprise - Full Optimisation -

    Program took 0.27 secs for 10000000 loops
    Program took 0.27 secs for 10000000 loops
    Program took 0.26 secs for 10000000 loops

    Dev C++, Mingw Compiler - Full Optimisation -

    Program took 0.14 secs for 10000000 loops
    Program took 0.14 secs for 10000000 loops
    Program took 0.13 secs for 10000000 loops

    Borland C++ 5.02 - Full Optimisation -

    Program took 0.35 secs for 10000000 loops
    Program took 0.34 secs for 10000000 loops
    Program took 0.35 secs for 10000000 loops

  11. #11
    Unregistered
    Guest
    0.88 Seconds

    AMD Athlon 700Mhz

    DJGPP 32 bit compiler

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > Concerning your timings I deduce after a quick search
    Yup, that's it.

    > Finally, do I take it that DJGPP is as fast as it gets or are there even faster options?
    gcc has a whole bunch of optimisations which are not turned on by -O2, because they tend to be code specific - sometimes they make things better, and sometimes they make things worse. As such, they require careful reading and experimentation.

    Other compilers are probably equally capable when it comes to optimisation.

    I hope that loop / case statement isn't in your code for real - the case is redundant.

  13. #13
    Unregistered
    Guest
    0.27 seconds - 0.22 seconds

    Pentium 3 900mhz
    MSVC++ 6 Enterprise (Full optimization)

    But I did manage to get it down to 0.05 - 0.11 seconds by editing the code a bit. If someone could take a look and see if my changes were valid, that would be nice.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<math.h>
    #include<time.h>
    
    #define LOOPS (10000000)
    
    int main(void){
      long j = 0;
      double a = 1000,temp;
      time_t cstart, cend;
    
      cstart=clock();
      while( j < LOOPS ){
        j++;
        if(j % 8)
          a = a * 1.001;
      }
      cend=clock();
      if( cend != cstart ){
        temp = ( (double)cend-cstart ) / CLK_TCK;
        printf( "Program took %-5.2f secs for %-lu loops", temp, LOOPS );
      }
      while( !kbhit() )
        ;
      return 0;
    }
    The biggest change was I noticed that only one of the cases was being used, so I changed it into an if. I also noticed that if(j & 8) gave the exact same output as if(j % 8). Is this because I wasn't looking at the correct output or are these two expressions the same?

  14. #14
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    DJGPP Athlon 800Mhz Win98 SE.
    optimised 0.03 seconds.
    normal 0.82 seconds.

  15. #15
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26
    Salem and Unregistered

    Thanks for the replies. The code example is NOT part of a real program as it achieves absolutely nothing! It specifically featured "switch()" as this is a crucial element in a large program I have written ( and am trying to speed up ). It was purely inteneded as a test of speed.

    No doubt there are much better benchmark programs about featuring pure computation with a good range of instructions - if somebody has one maybe they could post it here or start a new thread as speed of execution appears to be of interest to many but does not appear ( from my searches anyway ) to have been featured here before.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution speed test in c++
    By nick2price in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2009, 04:23 PM
  2. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  3. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  4. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  5. Flight Simulator Wind Speed!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 11:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21