Skip to content

Instantly share code, notes, and snippets.

@RenatoUtsch
Created November 28, 2012 17:44
Show Gist options
  • Save RenatoUtsch/4162799 to your computer and use it in GitHub Desktop.
Save RenatoUtsch/4162799 to your computer and use it in GitHub Desktop.
C Benchmark function
/*
* Implementation of the benchmark tool.
*/
#include "benchmark.h"
#ifndef __cplusplus
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#else
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#endif
/**
* Gets the current time.
* @return The current time, in seconds.
**/
double get_current_time();
double benchmark(void func(void *, size_t), void *data, size_t numBytes, int numBenchmarks)
{
double mean_time = 0.0; /** The time the benchmark ran. **/
double total_time = 0.0; /** The total time. **/
int i;
printf("----------------------------\nStarting benchmark...\n----------------------------\n");
for(i = 0; i < numBenchmarks; ++i)
{
double start_time = 0.0; /** The time the benchmark started. **/
double end_time = 0.0; /** The time the benchmark ended. **/
double bench_time = 0.0; /** end_time - start_time **/
/* Start the benchmark. */
start_time = get_current_time();
/* Call the function to benchmark. */
func(data, numBytes);
/* End the benchmark. */
end_time = get_current_time();
bench_time = end_time - start_time;
/* Add the benchmark time to the total time. */
total_time += bench_time;
/* Add the benchmark time to mean_time and if it wasn't 0, divide by 2. */
if(mean_time)
mean_time = (mean_time + bench_time) / 2;
else
mean_time = bench_time;
/* Print the current step. */
printf("Completed step %d\tTime spent: %fs\n", i + 1, bench_time);
}
/* Gobal time spent in the benchmark. */
printf("----------------------------\nEnded benchmark!\n");
printf("Total time spent: %fs\n", total_time);
printf("Mean time spent: %fs\n----------------------------\n", mean_time);
/* Return the mean time. */
return mean_time;
}
/* Implement get_current_time() depending on the OS. */
#if defined(WIN32) || defined(__WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN32_)
#include <windows.h>
double get_current_time() {
LARGE_INTENGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
#include <sys/time.h>
#include <sys/resource.h>
double get_current_time() {
struct timeval t;
gettimeofday(&t, 0);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
/*
* Benchmark tool, for Win32/POSIX, fully C89 compliant.
*/
#ifndef BENCHMARK_H_DEFINED
#define BENCHMARK_H_DEFINED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
/**
* Benchmark what is done in the function passed on the first parameter.
* @param func The callback function to call when the benchmark is started.
* @param data The data to pass to the callback function.
* @param numBytes The number of bytes of the data passed.
* @param numBenchmarks The number of benchmarks to be done to return a more accurate result (recommended at least 100).
* @return The time in seconds the function ran.
**/
double benchmark(void func(void *, size_t), void *data, size_t numBytes, int numBenchmarks);
#ifdef __cplusplus
}
#endif
#endif
@ryanavella
Copy link

I get an undefined identifier warning when compiling. I think you meant "LARGE_INTEGER" instead of "LARGE_INTENGER."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment