यहां मापने वाला कोड है जिसके साथ मैं आया हूं। क्या आप इसके साथ कोई समस्या देखते हैं? लिनक्स और विंडोज पर
निर्माण अब तक, संकलन के साथ:
g++ exception_handling.cpp -o exception_handling [ -O2 ]
या उदाहरण Visual C++ Express के लिए। MSVC में
g++ exception_handling.cpp -o exception_handling [ -O2 ] -fno-exceptions -DNO_EXCEPTIONS
या इसी तरह की सेटिंग:
आधार मामले ("अपवाद समर्थन पूरी तरह से हटा दिया भाषा") प्राप्त करने के लिए, का उपयोग करें।
कुछ प्रारंभिक परिणामों here। वे शायद क्योंकि मशीन लोड बदलती के सभी सस्ती हैं, लेकिन वे रिश्तेदार अपवाद हैंडलिंग भूमि के ऊपर के बारे में कुछ विचार दे कर। (कार्यकारी सारांश: कोई नहीं या थोड़ा जब कोई अपवाद नहीं फेंक दिया जाता है, बहुत बड़ा है जब वे वास्तव में फेंक दिया जाता है।)
#include <stdio.h>
// Timer code
#if defined(__linux__)
#include <sys/time.h>
#include <time.h>
double time()
{
timeval tv;
gettimeofday(&tv, 0);
return 1.0 * tv.tv_sec + 0.000001 * tv.tv_usec;
}
#elif defined(_WIN32)
#include <windows.h>
double get_performance_frequency()
{
unsigned _int64 frequency;
QueryPerformanceFrequency((LARGE_INTEGER*) &frequency); // just assume it works
return double(frequency);
}
double performance_frequency = get_performance_frequency();
double time()
{
unsigned _int64 counter;
QueryPerformanceCounter((LARGE_INTEGER*) &counter);
return double(counter)/performance_frequency;
}
#else
# error time() not implemented for your platform
#endif
// How many times to repeat the whole test
const int repeats = 10;
// How many times to iterate one case
const int times = 1000000;
// Trick optimizer to not remove code
int result = 0;
// Case 1. No exception thrown nor handled.
void do_something()
{
++result;
}
void case1()
{
do_something();
}
// Case 2. No exception thrown, but handler installed
#ifndef NO_EXCEPTIONS
void do_something_else()
{
--result;
}
void case2()
{
try
{
do_something();
}
catch (int exception)
{
do_something_else();
}
}
// Case 3. Exception thrown and caught
void do_something_and_throw()
{
throw ++result;
}
void case3()
{
try
{
do_something_and_throw();
}
catch (int exception)
{
result = exception;
}
}
#endif // !NO_EXCEPTIONS
void (*tests[])() =
{
case1,
#ifndef NO_EXCEPTIONS
case2,
case3
#endif // !NO_EXCEPTIONS
};
int main()
{
#ifdef NO_EXCEPTIONS
printf("case0\n");
#else
printf("case1\tcase2\tcase3\n");
#endif
for (int repeat = 0; repeat < repeats; ++repeat)
{
for (int test = 0; test < sizeof(tests)/sizeof(tests[0]); ++test)
{
double start = time();
for (int i = 0; i < times; ++i)
tests[test]();
double end = time();
printf("%f\t", (end - start) * 1000000.0/times);
}
printf("\n");
}
return result; // optimizer is happy - we produce a result
}
स्रोत
2008-09-04 07:38:09
मुझे आपके प्लेटफ़ॉर्म पर परिणामों में दिलचस्पी होगी ... –
क्या आपने कोई परीक्षण करने का प्रयास किया था जहां आप कोई अपवाद प्राप्त करेंगे (पकड़ें (...))। यह सिर्फ एक अपवाद वर्ग को पकड़ने से अलग हो सकता है ... –