测试结果:
gcc7/gcc9:
优化:g++ foo.cc -o foo -O2 -fopenmp
malloc: 0ms
new: 900ms
vs2017:
优化:默认Release
new/malloc: 200ms
看上去vs的运行结果比较正常,怎么解释这个?
测试代码如下:
#include <chrono>
#include <iostream>
#include <string.h>
using namespace std::chrono;
void test_copy() {
const long N = 1 * 1024 * 1024 * 1024L;
char *src = (char *)malloc(N);
if (!src) {
printf("src null\n");
return;
}
char *dst = (char *)malloc(N);
if (!dst) {
printf("dst null\n");
return;
}
// char *src = new char[N];
// char *dst = new char[N];
{
auto begin = steady_clock::now();
#pragma omp parallel for
for (long i = 0; i < N; i++) {
src[i] = i;
}
auto end = steady_clock::now();
auto duration = duration_cast<milliseconds>(end - begin);
std::cout << "assign duration time: " << duration.count() << "ms\n";
}
auto begin = steady_clock::now();
memcpy(dst, src, N);
auto end = steady_clock::now();
auto duration = duration_cast<milliseconds>(end - begin);
std::cout << "memcpy duration time: " << duration.count() << "ms\n";
// delete [] src;
// delete [] dst;
free(src);
free(dst);
}
int main(int argc, char* argv[])
{
test_copy();
return 0;
}
--
FROM 220.202.218.*