~cpp
// 테스트
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
/* for linux/unix */
#include <time.h>
#include <sys/time.h>
static void printf_localtime()
{
struct tm * ttt;
struct timeval tv;
gettimeofday(&tv, NULL);
ttt = localtime(&tv);
printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d", ttt->tm_year, ttt->tm_mon, ttt->tm_mday, ttt->tm_hour, ttt->tm_min, ttt->tm_sec, tv.tv_usec/1000);
}
static unsigned int get_clock()
{
struct timeval tv;
unsigned int t;
gettimeofday(&tv, NULL);
t = ((tv.tv_sec % 1000000) * 1000) + (tv.tv_usec / 1000);
return t;
}
#else
/* for windows */
#define get_clock clock
#endif
#ifdef __BIGENDIAN__
# define FTOI_LONG reserved, l
#else
# define FTOI_LONG l
#endif
#define USE_FTOI union{ double r; struct { long FTOI_LONG; } l; } __ftoitmp;
#define OP_FTOI(val) \
( ( (__ftoitmp.r=(val)+((((65536.*65536.*16.)+(65536.*.5))*65536.)) ), \
__ftoitmp.l.l-0x80000000L ) )
#define USE_FTOI2 union{ double r; struct { long FTOI_LONG; } l; } __ftoitmp;
#define LOOPCNT1 10
#define LOOPCNT2 10000000
void conv1(double *dv, long *lv)
{
*lv = *dv;
}
void test1()
{
double dval = 123456789.12345678;
long lval = 0;
long total = 0;
long start = 0, finish = 0;
long avg = 0;
long i=0, j=0;
start = get_clock();
for(j=0; j<LOOPCNT1; j++) {
for(i=0; i<LOOPCNT2; i++) {
conv1(&dval, &lval);
}
}
finish = get_clock();
total = finish - start;
avg = total / LOOPCNT1 / LOOPCNT2;
printf("1.... value %ld total %ld ms, avg %ld ms\n", lval, total, avg);
}
void conv2(double *dv, long *lv)
{
USE_FTOI
*lv = OP_FTOI(*dv);
}
void test2()
{
double dval = 123456789.12345678;
long lval = 0;
long total = 0;
long start = 0, finish = 0;
long avg = 0;
long i=0, j=0;
start = get_clock();
for(j=0; j<LOOPCNT1; j++) {
for(i=0; i<LOOPCNT2; i++) {
conv2(&dval, &lval);
}
}
finish = get_clock();
total = finish - start;
avg = total / LOOPCNT1 / LOOPCNT2;
printf("2.... value %ld total %ld ms, avg %ld ms\n", lval, total, avg);
}
void conv3(double *dv, long *lv)
{
register USE_FTOI
*lv = OP_FTOI(*dv);
}
void test3()
{
double dval = 123456789.12345678;
long lval = 0;
long total = 0;
long start = 0, finish = 0;
long avg = 0;
long i=0, j=0;
start = get_clock();
for(j=0; j<LOOPCNT1; j++) {
for(i=0; i<LOOPCNT2; i++) {
conv3(&dval, &lval);
}
}
finish = get_clock();
total = finish - start;
avg = total / LOOPCNT1 / LOOPCNT2;
printf("3.... value %ld total %ld ms, avg %ld ms\n", lval, total, avg);
}
int main(int argc, char *argv[])
{
test1();
test2();
test3();
return 0;
}
~cpp
// 결과값
-O0
1.... value 123456789 total 1750 ms, avg 0 ms
2.... value 123456789 total 921 ms, avg 0 ms
3.... value 123456789 total 1579 ms, avg 0 ms
-O1
1.... value 123456789 total 1671 ms, avg 0 ms
2.... value 123456789 total 829 ms, avg 0 ms
3.... value 123456789 total 828 ms, avg 0 ms
-O2
1.... value 123456789 total 1656 ms, avg 0 ms
2.... value 123456789 total 1047 ms, avg 0 ms
3.... value 123456789 total 1031 ms, avg 0 ms
-O3
1.... value 123456789 total 62 ms, avg 0 ms
2.... value 123456789 total 203 ms, avg 0 ms
3.... value 123456789 total 188 ms, avg 0 ms