"Jakub Moskal" <ja**********@gmail.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com...
Hi,
I want to write a benchmark that will measure performance of several
different algorithms for the same problem. There is a set time bound
though, the algorithm cannot run longer than n minutes.
Here is what the main program would do:
initializeVariables();
try
{
Timer.start(int n);
performAlgorithm_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTime();
}
catch (OutOfTimeException e)
{
cout << "Algorithm didin't finish in" << Timer.getN() << " minutes.";
}
The only way I see the timer to work and throw exception is to run it
as a thread in the background. If so, how do I stop the timer thread?
Any other ideas how to approach this problem?
Jakub
I don' t think this will work the way you expect it to. I don't think the
exception can be caught by
your catch block since the exception would be thrown in a different thread.
Threads are not covered
by the C++ standard, so you might want to experiment to see if I'm right or
not.
There are a couple of ways to handle this problem:
1. Instrument the algorithm code to periodically check the time elapsed
since the start.
2. Instrument the algorithm code to check an "abort" flag which will be set
by a separate timer
thread.
3. Run the algorithm code in a separate thread and terminate the thread
after the max time has elapsed.
This might be platform specific, it would work for windows, but I'm not
familar with the threading
libraries on linux/unix.
4. Spawn off algorithms in their own processes, and monitor the process for
time. Kill the process when
the elapsed time is exceeded. This would be fairly easy to do on
Linux/Unix as a shell script, might
be a bit more involved with windows, either use Perl to monitor or
write a C++ program to do the
monitoring/killing.
dave