Timing program

This is a discussion on Timing program within the C Programming forums, part of the General Programming Boards category; I just had a go at writing a program that times simple operations. It dosent seem to be working tho. ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Timing program

    I just had a go at writing a program that times simple operations. It dosent seem to be working tho. I figured that this should take at least 1 millisecond to run:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    int main()
    {
        srand(time(NULL));
        int *table1, *table2;
        unsigned x, y, z;
        unsigned time;
        int mod, mul, add, sub, div;
        int test;
        
        table1=malloc(sizeof(int)*1000000);
        table2=malloc(sizeof(int)*1000000);
        
        while(*table1) 
        {
            *table1=rand()+1;
            table1++;
        }
        while(*table2) 
        {
            *table2=rand()+1;    
            table2++;
        }   
         
        time=clock();
        while(*table1)
        {
            for(x=0; x<4294967295; x++) 
                for(y=0; y<4294967295; y++) 
                    for(z=0; z<4294967295; z++) 
                        test = *table1 % *table2;
            table1++; 
            table2++;
        }  
        mod=clock()-time;
        
        
        printf("Modulus: %i\n", mod);
        printf("X:%i Y:%i Z:%i", x, y, z);
        getchar();                 
    }
    It dident, so I printed out the x, y, z variables and it seems as if the loops have just decided to exit at random times. I must be doing something wrong here. Right?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
      while(*table1)
    It looks like you're assuming that malloc() initializes all the entries in the array table1 to nonzero values and puts a zero at the end. In fact, you can't assume anything about the values in the array before you initialize them. Same for table2.

    Edit: Probably you work with C strings a lot and forgot this wasn't one?
    Last edited by robatino; 08-13-2007 at 07:17 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > while(*table1)
    This is so very wrong.
    1. You're modifying the pointer to your memory - which will make it harder to free the memory later on.
    2. The memory returned by malloc is uninitialised, so examining it's value undefined.
    Just use a loop variable and treat it like an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Malloc usually returns a bunch of memory that contains zero, so I expect loops similar to this:
    Code:
        while(*table1) 
        {
            *table1=rand()+1;
            table1++;
        }
    to not do anything, because the first test of "*table" is zero, so it doesn't enter the first iteration of the loop.

    You may want something like:
    Add this early on in the code
    Code:
    #define ARRAY_SIZE 1000000
    Then replace this:
    Code:
        table1=malloc(sizeof(int)*1000000);
        table2=malloc(sizeof(int)*1000000);
        
       while(*table1) 
        {
            *table1=rand()+1;
            table1++;
        }
        while(*table2) 
        {
            *table2=rand()+1;    
            table2++;
        }   
         
        time=clock();
        while(*table1)
        {
            for(x=0; x<4294967295; x++) 
                for(y=0; y<4294967295; y++) 
                    for(z=0; z<4294967295; z++) 
                        test = *table1 % *table2;
            table1++; 
            table2++;
        }
    with
    Code:
        int i;
    
        table1=malloc(sizeof(int)*ARRAY_SIZE);
        table2=malloc(sizeof(int)*ARRAY_SIZE);
        
    
       for(i = 0; i < ARRAY_SIZE; i++)) 
        {
            table1[i]=rand()+1;
            table2[i]=rand()+1;    
        }
         
        time=clock();
        for(i = 0; i < ARRAY_SIZE; i++)
        {
            for(x=0; x<4294967295; x++) 
                for(y=0; y<4294967295; y++) 
                    for(z=0; z<4294967295; z++) 
                        test = *table1 % *table2;
            table1++; 
            table2++;
        }
    I also expect that 2^32^3*1000000 will take a bit longer than 1ms. 1 loop of 2^32 using divide takes about 30 seconds or so. 30^3 will be 240 seconds, times 1 million is about 80 years or so (I could have got that entirely wrong, but I'm sure my calculation is several orders of magnitude closer than the 1ms you are talking of).

    --
    Mats

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by matsp View Post
    Malloc usually returns a bunch of memory that contains zero, so I expect loops similar to this:
    No, malloc() does not initialise the space it allocates, as already said by Salem.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cwr View Post
    No, malloc() does not initialise the space it allocates, as already said by Salem.
    The standard does not SAY what malloc returrns, that is correct. However, it is QUITE COMMON that it returns memory that is all zeros - there are of course several other bugs in the code, which makes it fall over in a big heap - and of course, the time it will take is many orders of magnitude greater than the original poster suggests.

    --
    Mats

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another point: The compiler may well eliminate the x, y and z loops, as the pointer table doesn't change within the loop, so the loop actually doesn't make any difference - and test is only actually set to the final value of the loop, so the compiler may just do:

    Code:
    table1 += 1000000-1;
    table2 += 1000000-1;
    i = 1000000;
    test = *table1 % *table2;
    This would run in much less than 1ms, of course.

    It depends on how clever the compiler is, and whehter it has optimization enabled, what actually ends up in the code.

    --
    Mats

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Haha, yeah I screwed up bad there. Just spent all night trying random things to fix the clipping on a raycaster I'm making in Blitz Basic. Guess my brain turned to mush and I forgot how C works. Thanks guys, I should be able to get it to work now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timing my program
    By chico1st in forum C Programming
    Replies: 3
    Last Post: 08-16-2007, 01:00 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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