SE250:lab-1:dcho040
Jump to navigation
Jump to search
How long does it take to execute an addition (+) operation in C?
Information
- Variable sizes
Int : -2,147,483,648 to 2,147,483,647 Long : -2,147,483,648 to 2,147,483,647 Short : -32,768 to 32,767 float : (+-)1.175494e-38 to (+-)3.402823e+38 double: (+-)2.225074e-308 to (+-)1.797693e+308
- Clock function
- In the file 'time.h'
- Use type 'clock_t'
Expectation
- The time of running time would be effected by the range of each type
Expected order by the speed
- 1: short
- 2: int and long
- 3: float
- 4: double
Testing codes
Sample1
- Codes
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <time.h> int main(void) { int a; int b=1; clock_t t0 = clock(); for (a=1; a<1000; a++) { b = b+a; } printf("%d\n", b); printf("%d", clock() - t0); return 0; }
- Results
b = 499501 time = 0
- discussion
The time is too small -> more calculations are needed
Sample2
- Codes
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <time.h> int main(void) { int a; int b=1; clock_t t0 = clock(); for (a=1; a<1000000000; a++) { b = b+a; } printf("%d\n", b); printf("%d", clock() - t0); return 0; }
- Results
b = -1243309311 Time = 3475
- Discussion
B = -1243309311 because B is bigger than the bigger than 2,147,483,647(maximum int value) -> All calculation should be calculated with short size (the smallest variable: -32,768 to 32,767)
Sample3
- Codes
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <time.h> int main(void) { int a, b; int c=0; clock_t t0 = clock(); for (a=1; a<32767; a++) { for(b=1; b<32767; b++) { c = c+a-b; } } printf("c needs to be 0 = %d\n", c); printf("time = %d", clock() - t0); return 0; }
- Results
c = 0 Time = 3892
- discussion
While calculate the answer, 'c' goes over 32767, yet this looks fine.
Sample4(final)
- Codes
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <time.h> int main(void) { int a, b, c=1, d=0; clock_t t0 = clock(); for (a=1; a<32767; a++) { for(b=1; b<32767; b++) { d=d+c; } c=c*-1; } printf("c needs to be 32767 = %d\n", d); printf("time = %d", clock() - t0); return 0; }
- Results
c = 32767 Time = 2627
- Discussion
- This example includes ‘*’, ‘+’, and ’-‘ which increase the processing time, yet this lab suggests to use addition (+) operation.
Testing
Vista(Visual Studio 2008)
Type Int long Short Float Double -------------------------------------- Test1 2346 2334 3011 9074 9397 Test2 2463 2339 3251 9295 9122 Test3 2447 2343 3086 9029 9054 Test4 2337 2373 2981 9056 9053 Test5 2346 2336 2991 9026 9057 -------------------------------------- AVG 2388 2345 3064 9096 9137
- Discussion
- The results are not always same.
- The speed of a programme is may affected by the condition of the CPU and other running programmes.
- ‘int’ and ‘long’ are similar, and are the fastest.
- ’short’ is little slower than ‘int’ and ‘long’
- ’float’ and ‘double’ are similar, and about 3 times slower than ‘short’
- Data types, ‘float’ and ‘double’, have different range, but floating point data may types show same processing speed.
Vista(CYGWIN)
Type Int long Short Float Double -------------------------------------- Test1 3588 3541 6615 5102 5085 Test2 3540 3603 6723 5132 5132 Test3 3557 3603 6614 5070 5085 Test4 3541 3525 6693 5070 5101 Test5 3572 3556 6661 5116 5085 -------------------------------------- AVG 3560 3566 6661 5098 5098
- Discussion
- ‘int’ and ‘long’ are similar, and are the fastest.
- ’float’ and ‘double’ are similar, and are slower than ‘int’ and ‘long’
- The difference between ‘float’ and ‘double’, and ‘int’ and ‘long’ are smaller than in Visual Studio 2005.
- ’Short’ is slower than ‘float and ‘double which is unexpected.
- The type, ’short’, may uses difference processing method than ‘int’, and ‘long’
Conclusion
- The speed of a programme is may changeable by condition of the CPU and other running programmes.
- Same range data types may show similar processing speed (e.g. int and long).
- Floating point data types may show similar processing speed (e.g. float and double).
- This report suggests some rules with the time, but these rules could be different in other conditions. (e.g. 64bit windows, Linux, different cpus, etc)