//-------------------------------------------------------------------------------------------
#include <Wy.stdio.h>
#include <Wy.time.h>
using namespace Wy;
int ret_int()
{ return 3; };
Errno ret_errno()
{ return 3; };
void throw_int()
{ throw (int)3; };
const int Loops=40000000;
Timespec t0()
{
Timespec tm0,tm1;
tm0=Wy::now(CLOCK_THREAD_CPUTIME_ID);
for(int i=Loops; i; --i) {
int r=ret_int(); // Compiler warning: not used (Ok, meaningful)
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
r=ret_int();
}
tm1=Wy::now(CLOCK_THREAD_CPUTIME_ID);
return tm1-tm0;
};
Timespec t1()
{
Timespec tm0,tm1;
tm0=Wy::now(CLOCK_THREAD_CPUTIME_ID);
for(int i=Loops; i; --i) {
Errno r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
r=ret_errno();
}
tm1=Wy::now(CLOCK_THREAD_CPUTIME_ID);
return tm1-tm0;
};
Timespec t2()
{
Timespec tm0,tm1;
const int ScaleDown=100;
tm0=Wy::now(CLOCK_THREAD_CPUTIME_ID);
for(int i=Loops/ScaleDown; i; --i) {
int r; // Compiler warning: not used (Ok, meaningful)
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
try { throw_int(); } catch(int e) { r=e; };
}
tm1=Wy::now(CLOCK_THREAD_CPUTIME_ID);
{// Compute ScaleDown*(tm1-tm0)
Errno r;
tm1-=tm0;
const VLInt<unsigned int> Giga(Giga.itv(1000000000ul));
VLInt<unsigned int> a,b;
a=a.itv(static_cast<uint64_t>(tm1.second()));
a*=Giga;
a+=a.itv(static_cast<unsigned long>(tm1.nano()));
a*=a.itv(static_cast<unsigned int>(ScaleDown));
r=a.div(Giga,b);
if(r!=Ok) {
WY_THROW( Reply(r) );
}
if((a.num_digits()!=1)||(b.num_digits()!=1)) {
WY_THROW( Reply(r) );
}
if((r=tm1.reset(a._arr().front(),b._arr().front()))!=Ok) {
WY_THROW( Reply(r) );
}
}
return tm1;
//return ScaleDown*(tm1-tm0);
};
int main(int argc, char* argv[])
try {
cout << "Measuring...." WY_ENDL;
const Timespec tm0= t0();
const Timespec tm1= t1();
const Timespec tm2= t2();
cout << "Return int = " << tm0 << WY_ENDL
<< "Return Errno = " << tm1 << WY_ENDL
<< "Throw int = " << tm2 << WY_ENDL
;
cout << "OK" WY_ENDL;
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "Unknown throw type" WY_ENDL;
throw;
};
//----------------------------------------------------------------------
[]$ ./sp_throw
Measuring....
Return int = 0.595733589
Return Errno = 1.039364795
Throw int = 539.335618700
OK
------------- (compiled with -O2)
[]$ ./sp_throw
Measuring....
Return int = 0.482806556
Return Errno = 0.519995772
Throw int = 556.814217900
OK
//------------------------------------------------------------------------
Throwing int is approximately 1000 times slower than returning int.
IIRC, the time ratio is not changed (nearly 20 years).