Voor school moesten we een klein scriptje schrijven om dingen te timen. Nu hadden we daar geen problemen mee, maar hetgeen wat we hebben getimed vonden we een beetje vreemd.
Script:
Output mijzelf (gcc, amd turion 64, 32 bit Ubuntu)
Output collega PowerPC (Mac OS X)
Hoe kan dit verschil? Normaal zijn integeroperaties toch altijd sneller dan floating-point? Wie kan mij vertellen hoe het kan dat bij mij integeroperaties bijna 4x zolang duren?
Script:
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| #include<stdio.h> #include<sys/time.h> #include<stdlib.h> #define LOOPS 100000000 void sum_integer(){ int sum=0, k=1, m=1; int i; for(i=1; i<=LOOPS; i++){ m = k * k; sum = sum + m / k; } } void sum_float(){ float sum=0, k=1, m=1; int i; for(i=1; i<=LOOPS; i++){ m = k * k; sum = sum + m / k; } } void sum_double(){ double sum=0, k=1, m=1; int i; for(i=1; i<=LOOPS; i++){ m = k * k; sum = sum + m / k; } } long giveMiliseconds(struct timeval *tm1, struct timeval *tm2){ long segundos = tm2->tv_sec - tm1->tv_sec; long micros = tm2->tv_usec - tm1->tv_usec; if(micros < 0){ segundos-=1; micros+=1000000; } micros = (long) ((float)micros /1000); return segundos*1000 + micros; } int main(void){ struct timeval tm1, tm2; struct timezone tz1, tz2; gettimeofday(&tm1, &tz1); sum_integer(); gettimeofday(&tm2, &tz2); printf("INTEGERS: %ld (ms)\n",giveMiliseconds(&tm1, &tm2)); gettimeofday(&tm1, &tz1); sum_float(); gettimeofday(&tm2, &tz2); printf("FLOATS: %ld (ms)\n",giveMiliseconds(&tm1, &tm2)); gettimeofday(&tm1, &tz1); sum_double(); gettimeofday(&tm2, &tz2); printf("DOUBLES: %ld (ms)\n",giveMiliseconds(&tm1, &tm2)); } |
Output mijzelf (gcc, amd turion 64, 32 bit Ubuntu)
INTEGERS: 3158 (ms) FLOATS: 826 (ms) DOUBLES: 870 (ms)
Output collega PowerPC (Mac OS X)
INTEGERS: 2719 (ms) FLOATS: 3953 (ms) DOUBLES: 3884 (ms)
Hoe kan dit verschil? Normaal zijn integeroperaties toch altijd sneller dan floating-point? Wie kan mij vertellen hoe het kan dat bij mij integeroperaties bijna 4x zolang duren?