Register loop weirdness

This is a discussion on Register loop weirdness within the C Programming forums, part of the General Programming Boards category; Hello, With the following code I get some strange results. Am I doing something wrong? Code: #include <stdio.h> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Question Register loop weirdness

    Hello,

    With the following code I get some strange results. Am I doing something wrong?

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    int main(void) {
      unsigned int i;
      register unsigned int j;
      clock_t start, finish;
    
      start = clock();
      for (i = 0; i < 32000; i++);
      finish = clock();
      printf("\nNon-register loop: %ld ticks\n", finish - start);
      printf("Start = %ld\n", start);
      printf("Finish = %ld\n", finish);
    
      start = clock();
      for (j = 0; j < 32000; j++)
      finish = clock();
      printf("\nRegister loop: %ld ticks\n", finish - start);
      printf("Start = %ld\n", start);
      printf("Finish = %ld\n", finish);
    
      printf("\n");
    
      return 0;
    }
    Here's the output on my machine, compiled with gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3:

    Non-register loop: 0 ticks
    Start = 0
    Finish = 0

    Register loop: 20000 ticks
    Start = 0
    Finish = 20000

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Probably nothing wrong with it. You should look up the constant CLOCKS_PER_SEC (also in time.h). In Microsoft Visual Studio it's 1000. That is, 1000 clocks per second. No mention of what the true resolution is though. It may be that clock() is not a very fine-grained way to measure time.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Look carefully at these two lines:
    Quote Originally Posted by Q345 View Post
    Code:
      for (i = 0; i < 32000; i++);
    
      for (j = 0; j < 32000; j++)
    See the difference?

    (In your second loop, because of the missing semicolon, you are calling clock() 32000 times. That definitely slows things down. Also, remember that compilers have a habit of optimizing empty loops into nothing -- your first loop is empty, so it vanishes. The second loop isn't, so it doesn't.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    With the semi-colon fixed, you really should just get 0 here since that loop (1-32000) could probably be executed millions of times per second, ie, it is not even one tick.

    CLOCKS_PER_SEC on linux is 1000000. If you want the actual number of ticks on your computer, it is measured by the kernel at boot. Search through /var/log/dmesg or /var/log/messages for a line like this:

    [ 0.000999] Calibrating delay using timer specific routine.. 4398.97 BogoMIPS (lpj=2199489)

    the "lpj" (loops per jiffie) is the actual number of ticks per second. However, you will need a for() loop into the billions before you take up anything like a second. Why a "tick" is apparently more than one instruction I don't know.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Smile DOH!

    Just goes to show you can look at your own code 50 times and not see anything wrong with it! One of my friends likes to have an "over-the shoulder" with a buddy when he can't figure something out. Guess this was one of those times...

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Double doh on me for not noticing semicolon problem. Sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. register variables
    By Draco in forum C Programming
    Replies: 7
    Last Post: 08-21-2002, 01:01 PM

Tags for this Thread


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