removing a loop cause it to go at half the speed?
- From: tom fredriksen <tom@xxxxxxxxxx>
- Date: Wed, 15 Mar 2006 17:14:38 +0100
Hi
I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to spend twice the time to complete. If the loop is included it takes about 7.48 seconds to complete, but when removed it takes about 11.48 seconds.
Does anybody have a suggestion as to why this is so and whether I can trust the results of the code as it is below?
/tom
The code was compiled on linux 2.6.3 with gcc 3.3.2 and glibc 2.2.3
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
int total = 0;
int count = 65500;
signed int data[count];
/* Initialising array loop */
for(int c=0; c<count; c++) {
data[c]=1+(int) (2000000000.0*rand()/(RAND_MAX+1.0));
}
struct timeval start_time;
struct timeval end_time;
gettimeofday(&start_time, NULL);
for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}
gettimeofday(&end_time, NULL);
double t1=(start_time.tv_sec*1000)+(start_time.tv_usec/1000.0);
double t2=(end_time.tv_sec*1000)+(end_time.tv_usec/1000.0);
printf("Elapsed time (ms): %.6lf\n", t2-t1);
printf("Total: %u\n", total);
return(0);
}
.
- Follow-Ups:
- Re: removing a loop cause it to go at half the speed?
- From: Dik T. Winter
- Re: removing a loop cause it to go at half the speed?
- From: Old Wolf
- Re: removing a loop cause it to go at half the speed?
- From: David Holland
- Re: removing a loop cause it to go at half the speed?
- From: laura fairhead
- Re: removing a loop cause it to go at half the speed?
- From: Vladimir S. Oka
- Re: removing a loop cause it to go at half the speed?
- From: Jan-Hinnerk Dumjahn
- Re: removing a loop cause it to go at half the speed?
- From: bert
- Re: removing a loop cause it to go at half the speed?
- From: Rod Pemberton
- Re: removing a loop cause it to go at half the speed?
- Prev by Date: Re: Idiotic question of an Idiot - dint like dont reply.
- Next by Date: Re: Unicode: ugh!
- Previous by thread: Please explain signal handler behaviour
- Next by thread: Re: removing a loop cause it to go at half the speed?
- Index(es):
Relevant Pages
|