views:

239

answers:

6

Hi there ,

I have ported a C program to a C++ Template Meta program .Now i want to compare the runtime . Since there is almost no runtime in the C++ program , how should i compare these 2 programs.
Can i compare C runtime with C++ compile time ? or is it just not comparable ?

+2  A: 

If I understand correctly, you've rewritten a C program with one that is entirely template-based? As a result, you're comparing the time it takes to run the C program with a C++ program that takes almost no time but simply writes the result out.

In this case, I don't think its quite comparable - the end user will see the C program take x seconds to run, and the C++ one complete immediately. However, the developer will see the C program compile in x seconds, and the C++ compile in many more seconds.

You could compare the C++ compile time to the C run time, and if the app is designed to produce a result and never run twice, then yes, you can compare the times in this way. If the program is designed to be run multiple times, then the run time is what you need to compare.

I just hope you put a LOT of comments in your C++ template code though :)

PS. I'm curious - how long does the C take to run, compared to the compile time for both?

gbjbaanb
+4  A: 

You can compare anything you want to compare. There is no one true rule of what should be compared.

You can compare the time each version takes to execute, or you can compare the time taken to compile each.

Or you can compare the length of the program, or the number of 'r' characters in the source file. You could compare the timestamp of each file.

How you should compare the two programs depend on what you want to show!

If you want to show that one executes faster than the other, then run both, time how long they take to execute, and compare those numbers.

If you want to show that one compiles faster than the other, then time the time it takes to compile them.

If you think the relation between the compile time of the C++ program and the run time of the C program is relevant, then compare those.

Decide what it is you want to show. Then you'll know what to compare.

jalf
+1  A: 

since the C++ program will always produce the same result, why bother with any of it? compute the result once using either program, and then replace both with:

int main()
{
   printf("<insert correct output here>\n");
   return 0;
}
Brian Postow
A: 

Today's C and C++ compilers share the same backends, hence generate most likely the same assembly code.

C++ is just a more annotated C, and you can still do good C while Cplusplusing ;)

C is just C++ old brother.

ZeroCool
You might want to try to read the question before you answer.
sbi
I bet he tried to read it and just got confused like the rest of us.
caspin
nope
ZeroCool
A: 

This is what i think you're trying to do:

You haven't said what your c program does so lets say it computes a cosine number to some specified degree of accuracy. You've converted this program into a c++ template-based equivalent which does the same thing but at compile time to yield a compile-time contant value. This is a reasonable thing to do as you may have an algorithm that uses "hard-coded" cosine values and you prefer not to have a table of random looking numbers. See this article for an example of real-world use for this (or do a search for Blitz and/or Todd Veldhuizen for more examples).

In which case, you therefore want to compare the compile-time performance of the C++ sine calculator against the run-time performance of the original C version.

A direct comparison of the time to compile the C++ source file against the time to run the C version will almost certainly show the compile time to be significantly slower. But this is hardly a fair comparison since the compiler is doing a lot more than just "executing" the template code.

EDIT: You could compensate for the compiler overhead by creating a copy of your c++ program which has some simple code equivalent to what the templated code would generate - i.e. you have to hand compile your templated code if that makes sense. If you then time the compilation of that source, the difference between that time and time to compile your original C++ templated program is presumably just the time required to execute the template.

jon hanson
What is the compiler doing except of executing?
n00ki3
Compiling :-) Reading the source file, pre-processing it, parsing it, generating the machine code and writing out the object file. I guess you could compensate for that - let me edit my replay above.
jon hanson
^replay^reply^
jon hanson
A: 

I think what would make sense is to compare compile times of the two programs, then runtimes, then you can calculate after how many runs you have amortized the additional compile time.