#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    22
    Rep Power
    0

    c - simple checksum/ code help


    hey all
    i wanna run a this piece of code but i cant work out what things i have to pass into it any help would be great. well what i have done is recorded the time ofday then i want to run this code (chksum) then record the time again compute the difference and see how long it took but the time stuff is easy just not sure what to do here with this bit of code to get it to run. i know i need to make a 1 mb buffer for it but not sure how

    ---------

    u_short
    cksum(u_short *buf, int count)
    {
    register u_long sum = 0;

    while (count--)
    {
    sum += *buf++;
    if (sum & 0xFFFF0000)
    {
    sum &= 0xFFFF;
    sum++;
    }
    }
    return ~ (sum & 0xFFFF);
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I don't really know what you are trying to accomplish, but will this help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    unsigned short cksum(unsigned short *buf, int count) { 
        register unsigned short sum = 0; 
    
        while (count--){ 
            sum += *buf++; 
            if (sum & 0xFFFF0000){ 
                sum &= 0xFFFF; 
                sum++; 
            } 
        } 
        return ~ (sum & 0xFFFF); 
    }
    
    
    int main(){
        unsigned short * buf, check;
        int count = 1000000;
        time_t t_start, t_stop;
    
        if ((buf = (unsigned short *) malloc(count * sizeof(unsigned short))) == NULL){
            fprintf(stderr, "Unable to allocate memory for buffer!\n");
            exit(1);
        }
    
        /* you might want to throw some random data in there */
        time(&t_start);/* the function time only has second resolution! */
        check = cksum(buf, count);
        time(&t_stop);
    
        printf("return value from cksum() is %d\n", check);
        printf("It took %d seconds to complete execution\n", t_stop - t_start);
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    22
    Rep Power
    0
    yeah i think thats it, thanks heaps man
    so is count the size of the data i am runin the checksum on ?
    i am still a bit new to this
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Yup. BTW: What is the purpose of your checksum?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    22
    Rep Power
    0
    so i can see what computer can do it it the quickest and at different times of day depending on it workload

IMN logo majestic logo threadwatch logo seochat tools logo