[英]Benchmarking - g++ produces different code from MSVC
I'm writing a benchmark, that measures speed and memory use of my program.我正在编写一个基准测试,用于测量我的程序的速度和内存使用情况。 I work in VS19, but my test will be run on a linux machine, so I need to compile it in g++.我在VS19上工作,但是我的测试会在linux机器上运行,所以我需要用g++编译它。 My problem is, that in VS19 my code produces correct results that I expect, compiled with g++ it produces wrong results.我的问题是,在 VS19 中,我的代码产生了我期望的正确结果,用 g++ 编译它产生了错误的结果。
The issue is in memory measurement.问题在于内存测量。 In Functions.cpp I have overloaded new and delete在 Functions.cpp 中,我重载了new和delete
AllocationMetrics s_allocationMetrics;
void* operator new(size_t size) {
//cout << "Allocate " << size << " bytes\n";
s_allocationMetrics.totalMemory += size;
return malloc(size);
}
void operator delete(void* ptr, size_t size) {
//cout << "Deallocate " << size << " bytes\n";
s_allocationMetrics.totalFree += size;
free(ptr);
}
and in Functions.h I have created this struct, that tracks the memory allocations在 Functions.h 中我创建了这个结构,它跟踪内存分配
struct AllocationMetrics {
uint64_t totalMemory = 0;
uint64_t totalFree = 0;
uint64_t getCurrentUsage() { return totalMemory - totalFree; }
uint64_t maxMem=0;
void setMaxMem() {
uint64_t temp= getCurrentUsage();
if (temp > maxMem)
maxMem = temp;
}
void resetMaxMem() { maxMem = 0; }
};
In the in functions from Functions.cpp, that I need measured, I call setMaxMem and in my Benchmark.cpp I call resetMaxMem , when appropriate.在我需要测量的 Functions.cpp 的 in 函数中,我调用setMaxMem ,在我的 Benchmark.cpp 中,我在适当的时候调用resetMaxMem 。 The struct is declared in Functionc.cpp, and in all other .cpp files it is declared as extern.该结构在 Functionc.cpp 中声明,在所有其他 .cpp 文件中声明为 extern。
For this example, I run my benchmark 6x in a loop and with Visual Studio's MSVC I get the expected memory results, with all possible settings (debug, release, etc).在这个例子中,我循环运行我的基准测试 6x,并使用 Visual Studio 的 MSVC 获得预期的内存结果,以及所有可能的设置(调试、发布等)。
Params: 1063 x 24 5 5 255
Sorting 0.0084792s 0.36439MB
Stripes 0.0179886s 1.64792MB
Params: 1063 x 24 5 5 255
Sorting 0.0080979s 0.364406MB
Stripes 0.0165975s 1.64794MB
Params: 1063 x 24 5 5 255
Sorting 0.0080586s 0.364422MB
Stripes 0.0163024s 1.64795MB
Params: 1063 x 24 5 5 255
Sorting 0.0081503s 0.364438MB
Stripes 0.016274s 1.64797MB
Params: 1063 x 24 5 5 255
Sorting 0.0079747s 0.364454MB
Stripes 0.0170647s 1.64799MB
Params: 1063 x 24 5 5 255
Sorting 0.0079046s 0.364486MB
Stripes 0.0160469s 1.64801MB
When I compile the same code with g++ compiler g++ Source.cpp Functionc.cpp benchmarking.cpp
, I get these wrong results当我用 g++ compiler g++ Source.cpp Functionc.cpp benchmarking.cpp
编译相同的代码时,我得到了这些错误的结果
Params: 1063 x 24 5 5 255
Sorting 0.063828s 1.0806MB
Stripes 0.171574s 3.73044MB
Params: 1063 x 24 5 5 255
Sorting 0.061869s 4.32227MB
Stripes 0.172318s 6.97212MB
Params: 1063 x 24 5 5 255
Sorting 0.067859s 7.56395MB
Stripes 0.170573s 10.2138MB
Params: 1063 x 24 5 5 255
Sorting 0.063863s 10.8057MB
Stripes 0.171541s 13.4555MB
Params: 1063 x 24 5 5 255
Sorting 0.062963s 14.0473MB
Stripes 0.170528s 16.6972MB
Params: 1063 x 24 5 5 255
Sorting 0.064827s 17.2891MB
Stripes 0.171541s 19.9389MB
Everything else is correct in g++ compilation, I tried different optimization levels, this memory bug didn't change.在 g++ 编译中其他一切都是正确的,我尝试了不同的优化级别,这个内存错误没有改变。 I use g++ (MinGW.org GCC-6.3.0-1) 6.3.0 version.我使用 g++ (MinGW.org GCC-6.3.0-1) 6.3.0 版本。 I also tried running this with clang, but same mistake appeared.我也尝试用 clang 运行它,但出现了同样的错误。
It appears as if the delete overload didn't take an effect with g++, or that delete isn't called when freeing variables.似乎删除重载对 g++ 没有影响,或者在释放变量时没有调用删除。 I tried compiling on both Windows and Linux (same version of g++), this bug appeared in both versions.我尝试在 Windows 和 Linux(相同版本的 g++)上编译,这个错误出现在两个版本中。
EDIT1 : forgot a line of code with temp definition. EDIT1 :忘记了一行带有临时定义的代码。
Thanks to Jarod42 for the useful comments.感谢Jarod42的有用评论。
The issue lies in GCC 6.3.0 choosing a different overload of the delete, namely delete(void*)
instead of delete(void*, size_t size)
which I have targeted.问题在于 GCC 6.3.0 选择了不同的删除重载,即delete(void*)
而不是我所针对的delete(void*, size_t size)
。 In order to have GCC behave I upgraded it to a newer 11.2.0 version.为了让 GCC 正常运行,我将其升级到更新的 11.2.0 版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.