-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemcpy-efficiency-test.cpp
More file actions
147 lines (115 loc) · 4.87 KB
/
memcpy-efficiency-test.cpp
File metadata and controls
147 lines (115 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <sstream>
#include <chrono>
#include <iomanip>
#include <locale>
#include <string>
#include "libs/FastMemcpy/FastMemcpy_Avx.h"
#include "SO_Serge_Rogatch.h"
#define NUMBER_ELEMENTS 0x10000000
#define BUFFER_ALIGNMENT_SIZE 32
class Foo
{
public:
template<class T>
static std::string FormatWithCommas( T value )
{
std::stringstream ss;
ss.imbue( std::locale( "" ) );
ss << std::fixed << value;
return ss.str();
}
};
int main()
{
const int loopCount = 100;
double * arrUnalignedSrc = new double[ NUMBER_ELEMENTS + BUFFER_ALIGNMENT_SIZE ]; // Create double size buffers
double * arrUnalignedDst = new double[ NUMBER_ELEMENTS + BUFFER_ALIGNMENT_SIZE ]; // to make sure desired
double * arrSrc = arrUnalignedSrc;
if ( (intptr_t( arrSrc ) & BUFFER_ALIGNMENT_SIZE - 1) != 0 )
{
arrSrc = reinterpret_cast< double * >((intptr_t( arrSrc ) & ~(BUFFER_ALIGNMENT_SIZE - 1)) + BUFFER_ALIGNMENT_SIZE);
}
double * arrDst = arrUnalignedDst;
if ( (intptr_t( arrDst ) & BUFFER_ALIGNMENT_SIZE - 1) != 0 )
{
arrDst = reinterpret_cast< double * >((intptr_t( arrDst ) & ~(BUFFER_ALIGNMENT_SIZE - 1)) + BUFFER_ALIGNMENT_SIZE);
}
double * pElem = arrSrc;
for ( int i = 0; i < NUMBER_ELEMENTS; ++i )
{
*pElem = static_cast< double >(i);
pElem++;
}
memset( arrDst, 0, NUMBER_ELEMENTS * sizeof( double ) / sizeof( int ) );
// Test 1: memcpy
//
// Copy once outside of loop
memcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
// Do the bulk of the test
auto start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < loopCount; i++ )
memcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
auto elapsed = std::chrono::high_resolution_clock::now() - start;
// Print result
double nSec = 1e-6 * std::chrono::duration_cast< std::chrono::microseconds >(elapsed).count();
printf( "memcpy : %s bytes/sec\n", Foo::FormatWithCommas( NUMBER_ELEMENTS * sizeof( double ) / nSec * loopCount ).c_str() );
// Test 2: memcpy_fast
//
memset( arrDst, 0, NUMBER_ELEMENTS * sizeof( double ) / sizeof( int ) );
// Copy once outside of loop
memcpy_fast( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
// Do the bulk of the test
start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < loopCount; i++ )
memcpy_fast( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
elapsed = std::chrono::high_resolution_clock::now() - start;
// Print result
nSec = 1e-6 * std::chrono::duration_cast< std::chrono::microseconds >(elapsed).count();
printf( "skywind3000/FastMemcpy::memcpy_fast : %s bytes/sec\n", Foo::FormatWithCommas( NUMBER_ELEMENTS * sizeof( double ) / nSec * loopCount ).c_str() );
//// Test 3: fastMemcpy
////
//memset( arrDst, 0, NUMBER_ELEMENTS * sizeof( double ) / sizeof( int ) );
//// Copy once outside of loop
//fastMemcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
//// Do the bulk of the test
//start = std::chrono::high_resolution_clock::now();
//for ( int i = 0; i < loopCount; i++ )
// fastMemcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
//elapsed = std::chrono::high_resolution_clock::now() - start;
//// Print result
//nSec = 1e-6 * std::chrono::duration_cast< std::chrono::microseconds >(elapsed).count();
//printf( "SO Serge Rogatch's Answer : %s bytes/sec\n", Foo::FormatWithCommas( NUMBER_ELEMENTS * sizeof( double ) / nSec * loopCount ).c_str() );
// Test 3: memcpy unaligned source
//
arrSrc = arrUnalignedSrc;
if ( (intptr_t( arrSrc ) & BUFFER_ALIGNMENT_SIZE - 1) == 0 )
{
arrSrc++;
}
// Copy once outside of loop
memcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
// Do the bulk of the test
start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < loopCount; i++ )
memcpy( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
elapsed = std::chrono::high_resolution_clock::now() - start;
// Print result
nSec = 1e-6 * std::chrono::duration_cast< std::chrono::microseconds >(elapsed).count();
printf( "memcpy (UNALIGNED SOURCE) : %s bytes/sec\n", Foo::FormatWithCommas( NUMBER_ELEMENTS * sizeof( double ) / nSec * loopCount ).c_str() );
// Test 2: memcpy_fast unaligned source
//
memset( arrDst, 0, NUMBER_ELEMENTS * sizeof( double ) / sizeof( int ) );
// Copy once outside of loop
memcpy_fast( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
// Do the bulk of the test
start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < loopCount; i++ )
memcpy_fast( arrDst, arrSrc, NUMBER_ELEMENTS * sizeof( double ) );
elapsed = std::chrono::high_resolution_clock::now() - start;
// Print result
nSec = 1e-6 * std::chrono::duration_cast< std::chrono::microseconds >(elapsed).count();
printf( "skywind3000/FastMemcpy::memcpy_fast (UNALIGNED SOURCE): %s bytes/sec\n", Foo::FormatWithCommas( NUMBER_ELEMENTS * sizeof( double ) / nSec * loopCount ).c_str() );
delete[] arrUnalignedSrc;
delete[] arrUnalignedDst;
}