#!/bin/sh

BIN="test-cpu-2"

run ()
{
echo --------------------------------------------------------------------------
CMD="gcc -x c - -o $BIN -lm -pg $*"
echo $CMD
$CMD <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

time_t seconds;
int i, r, numero_ciclos, numero_ciclosM, t1, t2;
clock_t start, end;

#define MY_(X,Y,Z) \
inline void my_ ## X () \
{ \
	numero_ciclos = 1000000000; \
	numero_ciclosM = numero_ciclos / 1E6; \
	start = clock (); \
	for (i = 0; i < numero_ciclos; i++) r = X (Z); \
	end = clock (); \
	printf ("%d millones de %s() en %.3f segundos (resultado de ej.: %d)\n", \
		numero_ciclosM, Y, (double)(end - start)/CLOCKS_PER_SEC, r); \
}


inline void randomize ()
{
	time(&seconds);
	srand((unsigned int) seconds);
}

MY_(rand, "rand", )
MY_(sqrt, "sqrt", i)
MY_(random, "random", )
MY_(log, "log", i)

int main(int argc, char ** argv)
{
	randomize ();

	my_rand ();
	my_sqrt ();
	my_log ();
	my_random ();

	return 0;
}
EOF
./$BIN
gprof $BIN gmon.out
rm -f $BIN gmon.out
}

run -O3
run -O3 -static

