Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Memcpy_Test.cpp
blobb360455cf73c81df025d45afe8ae291d5bc2f5a8
1 //=============================================================================
2 /**
3 * @file Memcpy_Test.cpp
5 * @author Mike Marinez <mmartinez@oci.com>
7 * This test compares the performance of ACE_OS::memcpy with
8 * that of smemcpy which unrolls the memcpy loop upto size = 16.
9 */
10 //=============================================================================
12 #include "ace/OS_NS_string.h"
13 #include "ace/High_Res_Timer.h"
15 #include "test_config.h"
17 void *
18 smemcpy (void *dest, const void *src, const size_t n)
20 unsigned char *to = static_cast<unsigned char*> (dest);
21 const unsigned char *from = static_cast<const unsigned char*> (src);
22 // Unroll the loop...
23 switch (n) {
24 case 16: to[ 15] = from[ 15];
25 case 15: to[ 14] = from[ 14];
26 case 14: to[ 13] = from[ 13];
27 case 13: to[ 12] = from[ 12];
28 case 12: to[ 11] = from[ 11];
29 case 11: to[ 10] = from[ 10];
30 case 10: to[ 9] = from[ 9];
31 case 9: to[ 8] = from[ 8];
32 case 8: to[ 7] = from[ 7];
33 case 7: to[ 6] = from[ 6];
34 case 6: to[ 5] = from[ 5];
35 case 5: to[ 4] = from[ 4];
36 case 4: to[ 3] = from[ 3];
37 case 3: to[ 2] = from[ 2];
38 case 2: to[ 1] = from[ 1];
39 case 1: to[ 0] = from[ 0];
40 case 0: return dest;
41 default: return ACE_OS::memcpy (dest, src, n);
45 void
46 testit (int type)
48 char buffer[16];
49 size_t size = 16;
51 switch (type)
53 case 0: smemcpy ((void*)buffer, (void*)" THIS IS A TEST", size); break;
54 case 1: ACE_OS::memcpy ((void*)buffer, (void*)" THIS IS A TEST", size); break;
59 namespace { enum { ITERATIONS = 100000000 }; }
61 int
62 run_main (int, ACE_TCHAR *[])
64 ACE_START_TEST (ACE_TEXT ("Memcpy_Test"));
66 //ACE_Time_Value start, now;
67 struct timeval start, now;
69 for (int i = ITERATIONS; i > 0; --i)
70 testit (0);
72 start = ACE_High_Res_Timer::gettimeofday_hr ();
73 for (int j = ITERATIONS; j > 0; --j)
74 testit (0);
76 now = ACE_High_Res_Timer::gettimeofday_hr ();
78 double fast = 1000000 *(now.tv_sec - start.tv_sec) +
79 now.tv_usec - start.tv_usec;
80 ACE_DEBUG ((LM_DEBUG,
81 ACE_TEXT ("%f uSec per iteration for fast version.\n"),
82 fast / ITERATIONS));
84 start = ACE_High_Res_Timer::gettimeofday_hr ();
85 for (int k = ITERATIONS; k > 0; --k)
86 testit (1);
88 now = ACE_High_Res_Timer::gettimeofday_hr ();
90 double slow = 1000000 * (now.tv_sec-start.tv_sec) + now.tv_usec - start.tv_usec;
91 ACE_DEBUG ((LM_DEBUG,
92 ACE_TEXT ("%f uSec per iteration for slow version.\n"),
93 slow / ITERATIONS));
95 ACE_END_TEST;
97 return 0;