planets / sqrt.c

#include <stdio.h>
#include <sys/time.h>
#include <math.h>

int main () {
    struct timeval tv1, tv2;
    struct timezone tz;
    
    int iterations = 10000;
    double i;
    double v = 1000.0;
    double y = 0.0;
   
    // First sqrt
    gettimeofday(&tv1,&tz);
    for(i = 0;i<iterations;i++) {
	y++;
	v = sqrt(y);
    }
    gettimeofday(&tv2,&tz);
    
    printf("Sqrt: %f us per sqrt\n", 
	   ((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
	    (tv2.tv_usec - tv1.tv_usec) )/iterations);
    

    // then exponent
    gettimeofday(&tv1,&tz);
    for(i = 0;i<iterations;i++) {
	y++;
	v = pow(y,0.51);
    }
    gettimeofday(&tv2,&tz);
    
    printf("Pow:  %f us per sqrt\n", 
	   ((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
	    (tv2.tv_usec - tv1.tv_usec) )/iterations);


    // then min
    gettimeofday(&tv1,&tz);
    for(i = 0;i<iterations;i++) {
	y++;
	v = (v < y ? v : y);
    }
    gettimeofday(&tv2,&tz);
    
    printf("Min:  %f us per sqrt\n", 
	   ((tv2.tv_sec - tv1.tv_sec) * 1000.0 * 1000.0 +
	    (tv2.tv_usec - tv1.tv_usec) )/iterations);

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.