#include #include #include #include // memdance.c by Tyler Bletsch (Tyler.Bletsch@duke.edu) for ECE250 // Benchmark the rate of basically random memory access to a block of memory for a given amount of a time double get_time() { struct timeval tv; gettimeofday(&tv, NULL); return (double)tv.tv_sec + tv.tv_usec/1000000.0;; } int main(int argc, char* argv[]) { size_t buffer_size=0; // bytes, given as an argument int execution_time; // seconds, given as argument int accesses_per_step = 1000000; // needs to be big enough that most of the time is spent hitting memory rather than computing time of day int idx_delta = 60029; // a fairly big prime so it won't wrap neatly for any common work size int steps = 0; // our counter if (argc != 3 || sscanf(argv[1],"%zd",&buffer_size)!=1 || sscanf(argv[2],"%d",&execution_time)!=1) { printf("Benchmark the rate of basically random memory access to a block of memory for a given amount of a time.\n\n"); printf("Syntax: %s \n",argv[0]); return 1; } double time_start = get_time(); char* buf = (char*) malloc(buffer_size); int idx = 0; // outer loop: keep going until we're out of time while (get_time() < time_start+execution_time) { // inner loop: do a bunch of accesses as fast as possible // this inner loop is here so the overhead of determining the time doesn't significantly affect our measurement for (int s=0; s