Re: How do you exception in your daily C++ programming? - test.cpp (0/1)
On Fri, 4 Sep 2009 07:35:47 CST, George Neuner <gneuner2@comcast.net>
wrote:
My test code is attached to this post.
Sorry everybody ... the moderators rejected the file attachment.
Here is the test code. As mentioned previously, it is Windows centric
using CPU performance counters ... you'll have to modify the timing
for your own platform. Also, 32-bit VS uses a separate abs64()
function for 64-bit ints.
Times are in seconds on a 3GHz dual Pentium 4. I timed the normal
return path and the exception return path (average of 1000 tests) for
call depths from 1 to 10 nesting levels.
Defines at the top control whether there is a local dtor object in the
nested call frames and whether the program is run at normal or high
priority (didn't matter much for my light loaded machine).
=====================================================
// test.cpp
//
#include <tchar.h>
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
/////////////////////////////////////////////
#define GARBAGE_SIZE 100
#define LOCAL_OBJECTS 1
#define PRIORITY 0
/////////////////////////////////////////////
class Garbage
{
private:
int *p;
public:
Garbage() { p = new int[GARBAGE_SIZE]; };
~Garbage() { delete [] p; };
};
/////////////////////////////////////////////
void func( int depth, bool excpt, __int64 *time )
{
#if LOCAL_OBJECTS
Garbage g;
#endif
if ( depth > 0 )
func( depth-1, excpt, time );
QueryPerformanceCounter( (LARGE_INTEGER*) time );
if ( excpt )
throw -1;
}
/////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
__int64 frequency, t1, t2, tEmpty, t_norm, t_xcpt;
double time_norm, time_xcpt;
int i, iter, depth;
#if PRIORITY
// set highest possible priority - above Windows OS
SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
SetThreadPriority( GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL );
#endif
// set output precision
cout << setprecision( 12 ) << setiosflags( ios::fixed );
iter = 1000;
// get counter frequency
QueryPerformanceFrequency( (LARGE_INTEGER*) &frequency );
// time empty call
QueryPerformanceCounter( (LARGE_INTEGER*) &t1 );
QueryPerformanceCounter( (LARGE_INTEGER*) &t2 );
tEmpty = t2 - t1;
// time calls with and without exception
// for various nesting depths
for ( depth = 1; depth <= 10; ++depth )
{
// time normal returns
t_norm = 0;
for ( i = 0; i < iter; ++i )
{
func( depth, false, &t1 );
QueryPerformanceCounter( (LARGE_INTEGER*) &t2 );
t_norm += (_abs64(t2 - t1) - tEmpty);
}
// exception return
t_xcpt = 0;
for ( i = 0; i < iter; ++i )
{
try
{
func( depth, true, &t1 );
}
catch (int)
{
}
QueryPerformanceCounter( (LARGE_INTEGER*) &t2 );
t_xcpt += (_abs64(t2 - t1) - tEmpty);
}
time_norm = ((double) t_norm / (double) iter) / (double)
frequency;
time_xcpt = ((double) t_xcpt / (double) iter) / (double)
frequency;
// print results
cout << "nesting depth = " << depth << endl
<< " normal return : " << time_norm << endl
<< " exception return: " << time_xcpt << endl;
}
return 0;
}
And a typical test run:
nesting depth = 1
normal return : 0.000000092797
exception return: 0.000007816391
nesting depth = 2
normal return : 0.000000078505
exception return: 0.000008222248
nesting depth = 3
normal return : 0.000000082720
exception return: 0.000008868277
nesting depth = 4
normal return : 0.000000093307
exception return: 0.000009373321
nesting depth = 5
normal return : 0.000000078848
exception return: 0.000009968914
nesting depth = 6
normal return : 0.000000079211
exception return: 0.000010930467
nesting depth = 7
normal return : 0.000000079184
exception return: 0.000011090618
nesting depth = 8
normal return : 0.000000102457
exception return: 0.000011629352
nesting depth = 9
normal return : 0.000000080039
exception return: 0.000012284262
nesting depth = 10
normal return : 0.000000094114
exception return: 0.000013034215
=====================================================
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]