Commit bba881b1 authored by Jeronimo Pellegrini's avatar Jeronimo Pellegrini

Include c++ files for the benchmark

Should have been included in the last commit.
parent f40eb6c0
#include <map>
#include <iostream>
#include "timer.h"
int main () {
std::map<unsigned int, std::map<unsigned int, std::map<unsigned int, double> > > mat;
unsigned a, i, j, k;
double x = 9.3;
double s = 0.0;
Timer t;
for (i=1; i<90; i++)
for (j=1; j<90; j++)
for (k=0; k<90; k++)
mat[i][j][k] = x;
std::cout << "start...\n";
t.start();
for (a=0; a<50; a++)
for (i=1; i<90; i++)
for (j=1; j<90; j++)
for (k=0; k<90; k++)
mat[i][j][k] = x;
t.stop();
// Calculate and print sum, so the compiler won't completely
// delete the loop:
for (i=1; i<90; i++)
for (j=1; j<90; j++)
for (k=0; k<90; k++)
s += mat[i][j][k];
std::cout << "Matrix sum: " << s << "\n";
std::cout << "Elapsed time (SET): "
<< t.elapsed_time()
<< " seconds\n";
t.reset();
t.start();
for (a=0; a<50; a++)
for (i=1; i<90; i++)
for (j=1; j<90; j++)
for (k=0; k<90; k++)
x = mat[i][j][k];
t.stop();
// Print x so the compiler won't completely delete the loop:
std::cout << "x = " << x << "\n";
std::cout << "Elapsed time (GET): "
<< t.elapsed_time()
<< " seconds\n";
}
#include <iostream>
#include <ctime>
extern "C" {
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
#include <math.h>
}
#include "timer.h"
/**
* Constructor.
*/
Timer::Timer() {
reset();
}
/**
* Resets the timer, setting elapsed time to zero.
*/
void
Timer::reset() {
struct tms time;
times( &time );
elapsed_t = 0;
start_t = time.tms_utime;
running = true;
}
/**
* Starts the timer again after stopping it.
*
* Elapsed time will NOT be set to zero!
*/
void
Timer::start() {
struct tms time;
times( &time );
start_t = time.tms_utime;
running = true;
}
/**
* Stops the timer.
*
* Does NOT clear elapsed time.
*/
void
Timer::stop() {
struct tms time;
times( &time );
clock_t current_t = time.tms_utime;
elapsed_t += current_t - start_t;
running = false;
}
/**
* Gives the elapsed time, in seconds.
*
* @return A double with the time.
*/
long double
Timer::elapsed_time() {
if (running) {
struct tms time;
times( &time );
clock_t current_t = time.tms_utime;
long double tot_secs = (long double) (elapsed_t + current_t - start_t) / (long double) sysconf(_SC_CLK_TCK);
return tot_secs;
} else {
return (long double) elapsed_t / (long double) sysconf(_SC_CLK_TCK);
}
}
/**
* Returns the current value of the clock, in clock_t.
*
* @return A clock_t value with the current time (as given by the times() C function call).
*/
clock_t
Timer::get_time() {
struct tms time;
times( &time );
return time.tms_utime;
}
#ifndef TIMER_H
#define TIMER_H
#include <ctime>
/**
* This class implements a simple timer.
*/
class Timer {
private:
clock_t start_t;
clock_t elapsed_t;
bool running;
public:
Timer();
void reset();
void start();
void stop();
long double elapsed_time();
clock_t get_time();
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment