Pushkar Pradhan said:
I want to time my matrix multiply code (in MFLOPS). I want to run the
code 100,000 times or some other big number.
This can be done 2 ways (for and while loops):
timer1 = time(NULL);
for(n = 0; n < 100000; n++)
mm();
timer2 = time(NULL);
exectime = difftime(timer2, timer1);
However, this means there will be a comparison of "n" on each
iteration and increment operation each time. I want to avoid this, if
you use while() same problem.
So is there someway to avoid the loop overhead, i.e. tell it:
do 100000 times{
mm
}
Pushkar Pradhan
Despite the fact that the loop counting you show
above will not likely have a noticeable impact on
your timing, I had fun writing this:
#include <stdio.h>
#include <stdlib.h>
#define PROTOTYPE "void mm();\n"
#define CALL " mm();\n"
#define ITERATIONS 100000
int main()
{
const char *code[] =
{
"#include <stdio.h>\n",
"#include <time.h>\n",
"\n",
PROTOTYPE,
"\n",
"int main(void)\n",
"{\n",
" time_t timer1 = time(NULL);\n",
" time_t timer2;\n",
" double exectime = 0;\n",
"\n",
"@",
"\n",
" timer2 = time(NULL);\n",
" exectime = difftime(timer2, timer1);\n",
" printf(\"Execution time %f seconds\\n\", exectime);\n",
" return 0;\n",
"}\n"
};
const int ret[] = {EXIT_SUCCESS, EXIT_FAILURE};
const size_t lines = sizeof code / sizeof *code;
size_t line = 0;
const unsigned long iters = ITERATIONS;
unsigned long iter = 0;
int call = 0;
int err = 0;
FILE *file = fopen("timeit.c", "w");
if(file)
for(line = 0; line < lines; ++line)
{
if(call = (*(code[line]) == '@'))
{
for(iter = 0; iter < iters; ++iter)
if(fputs(CALL, file) < 0)
break;
}
else
if(fputs(code[line], file) < 0)
break;
if(call && iter < iters)
break;
}
else
fprintf(stderr, "%s\n", "Cannot open output");
if(file && fclose(file) || (err = line < lines))
fprintf(stderr, "%s (line %lu)\n",
"Error writing output", (unsigned long)++line);
return ret[err];
}
Compile, link, and run this, compile the output
(file "timeit.c"), and link it with your 'mm()'
function. Of course you're still paying time
overhead for the call to 'mm()'. If you have
the source, you can apply this same method,
replacing the call to 'mm()' with its source,
and make a *really* big "timeit.c" file.
-Mike