SE250:lab-1:shua066

From Marks Wiki
Jump to navigation Jump to search

Lab1

this lab is to measure" how long does it take to ececute an additon (+) operation in C?"and use data type om C(int long short float double)

Firstly I try to declare variables ,write a loop to do it , use int ,double , for loop

 #include <stdio.h>
 #include<time.h>
 int main(void)
 {
    int a;
    int b=0;
    //try to make the number is big enough
     for (a=0;a<100000;a++){
        b=b+a;	
    }
    clock();
    printf("%d\n", b);
    return 0;
 }

But i got the wrong number , because the function clock() is in wrong way.

 #include <stdio.h>
 #include<time.h>
 int main(void)
 {
    int a;
    int b=0;
    clock_t t0=clock();
    for (a=0;a<1000000000;a++){
        b=b+a;	
    }
    printf("%ld\n", clock()-t0);
    return 0;
 }

I got

 2422

The answer looks OK,but the test is use other main data type ,and for ""short"", is not bigger than 32767,so I do again,and make sure the number is big enough,so i use two for loop to do it.

 #include <stdio.h>
 #include<time.h>
 int main(void)
 {
    int a;
    int b;
    int c=0;
    clock_t t0=clock();
    for (a=0;a<32767;a++){
        for(b=0;b<32767;b++){
            c=c+b-c;
        }	
    }
    printf("%ld\n", clock()-t0);
    return 0;
 }

and I got the answer is

 3406

so later i change the data type

  long     3421
  short    3421
  double   27375
  float    7234

All the data I collected from eng Lab (windows XP), and I did it again in GCL(windows vista) I got

  int      3750  
  long     3742 
  short    3774
  double   6155
  float    6146

And I try to did it again in FCL(iMac), I got the all the data type around

 340