I had prepared two sample code for showing thread having int variable calculation is faster than thread having double variable.
Only difference between two code is, in first i am using only integers and in other i am using only double.
Time difference between them is almost 30%.
Reason might be very simple/basic, but can anyone please give me the possible reason(s)?
Note: please ignore the logic of the code, because it is just prepared for demo.
Using integer :
#include <stdio.h>
#include <pthread.h>
pthread_t pth1,pth2,pth3,pth4;
void *threadfunc1(void *parm)
{
int i,j,k,l;
j = 0;
k = 0;
l = 5;
for (i = 0; i < 5000000; i ++) {
j = k + 152;
k = j + 21;
l = j + k + (j * 5) + (k * 2) + (l * 3);
j = k + ((l + j)/ k) + j + k + (l / k);
j = 0;
k = 0;
l = 5;
}
printf("Completed Thread 1\n");
return NULL ;
}
void *threadfunc2(void *parm)
{
int i,j,k,l;
j = 0;
k = 0;
l = 5;
for (i = 0; i < 5000000; i ++) {
j = k + 152;
k = j + 21;
l = j + k + (j * 5) + (k * 2) + (l * 3);
j = k + ((l + j)/ k) + j + k + (l / k);
j = 0;
k = 0;
l = 5;
}
printf("Completed Thread 2\n");
return NULL ;
}
int main () {
pthread_create(&pth1, NULL, threadfunc1, "foo");
pthread_create(&pth2, NULL, threadfunc2, "foo");
pthread_join( pth1, NULL);
pthread_join( pth2, NULL);
return 1;
}
Using double:
#include <stdio.h>
#include <pthread.h>
pthread_t pth1,pth2,pth3,pth4;
void *threadfunc1(void *parm)
{
double i,j,k,l;
j = 0;
k = 0;
l = 5;
for (i = 0; i < 5000000; i ++) {
j = k + 152;
k = j + 21;
l = j + k + (j * 5) + (k * 2) + (l * 3);
j = k + ((l + j)/ k) + j + k + (l / k);
j = 0;
k = 0;
l = 5;
}
printf("Completed Thread 1\n");
return NULL ;
}
void *threadfunc2(void *parm)
{
double i,j,k,l;
j = 0;
k = 0;
l = 5;
for (i = 0; i < 5000000; i ++) {
j = k + 152;
k = j + 21;
l = j + k + (j * 5) + (k * 2) + (l * 3);
j = k + ((l + j)/ k) + j + k + (l / k);
j = 0;
k = 0;
l = 5;
}
printf("Completed Thread 2\n");
return NULL ;
}
int main () {
pthread_create(&pth1, NULL, threadfunc1, "foo");
pthread_create(&pth2, NULL, threadfunc2, "foo");
pthread_join( pth1, NULL);
pthread_join( pth2, NULL);
return 1;
}