即使在最大优化下,如何强制未完成的代码部分运行?
我的程序中有一个函数可以执行一大堆浮点数学运算。它返回一个值数组,该数组当前尚未在我的程序中使用。
我想测试这段代码在最大优化下的速度,但是由于未使用该代码,编译器很方便地一起跳过该函数,我无法花时间在上面。
即使不使用结果,如何强制编译器在最大优化下运行该代码部分(我希望计算机让我了解该部分的运行速度)。
我正在运行 Visual C++ 2008。
I have a function in my program that preforms a whole bunch of floating point math. It returns an array of values which is not currently being used in my program yet.
I want to test this piece of code for speed under maximum optimizations, however since the code isn't used, the compiler conveniently skips the function all together and I can't get a time on it.
How do force the compiler to run that section of code under maximum optimizations even though the result is not used (I want the computer to just give me a sense as to how fast the section runs).
I'm running Visual C++ 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
SecureZeroMemory()
从函数接收到结果后覆盖结果。您甚至不需要覆盖整个结果,一个数组元素就足够了,也许您甚至可以将零作为“字节数”传递,这样函数就不会执行任何操作。
这将在 Windows 上发挥作用 -
SecureZeroMemory()
旨在永远不会被编译器优化。使用它非常简单而且速度相当快。You could use
SecureZeroMemory()
to overwrite the result after is has been received from the function. You don't even need to overwrite the whole result, one array element will be enough, maybe you can even pass zero as "number of bytes", so that nothing is done by the function.This will do the trick on Windows -
SecureZeroMemory()
is intended to never be optimized out by the compiler. Using it is pretty straightforward and it's rather fast.我确信有很多编译器技巧,但最简单的方法是让它看起来像您正在使用该值。在这种情况下,只需将返回的数组传递给其他函数即可。另一个函数不需要执行任何操作,但这应该足以让编译器相信您需要结果。
如果您发现空的第二个函数也被优化掉了,那么只需将其放入共享库(DLL)中,编译器就不可能知道它是如何使用的。
您如何分配结果也可以改变这一点。如果您向原始函数传递一个指针,则可以只向它传递一个堆指针。由于该指针可能在其他地方使用,因此编译器不太可能优化代码,因为它不知道结果是否会被使用。
您也可以合法地使用这些数据。在另一个函数中验证结果是有意义的。如果进行性能测试,只需将此验证部分放在计时部分之外即可。这通常是我进行此类性能测试的方式(确保检查/使用结果)。
I'm sure there are many compiler tricks, but the easiest way is to just make it look like you are using the value. In this case, just pass the returned array to some other function. The other function doesn't need to do anything, but that should be enough to convince the compiler you need the results.
If you find that your empty second function is being optimized out as well, then just stick it in a shared library (DLL) and it is impossible for the compiler to know how it is being used.
How you allocate the result can also change this. If you pass the original function a pointer, you could just pass it a heap pointer. Since that pointer may be used somewhere else it is highly unlikely the compiler could optimize away the code, as it has no idea if the results will be used or not.
You could also just legitimately use the data. It makes sense to verify the results in another function. If doing performance testing just put this verification part outside of the timed section. This is generally how I do such performance tests (make sure the result is checked/used).
这就是测试用例的用途。在单独的二进制文件中编写一个测试用例(即使只是在 main() 方法中),该测试用例将一次性局部变量设置为函数的结果。使用您喜欢的方法计算时间(例如,通过捕获分配之前和分配之后的时间(NULL)并打印时间差)。您应该对运行时间有一个不错的了解。
编辑:时间(NULL)是整秒精度=坏和邪恶。使用clock(),如此处所示,以获得 C/C++ 标准库中最准确的精度。
This is what a test case is for. Write a test case in a separate binary (even just in the main() method) which sets a throwaway local variable to the result of the function. Time using your preferred method (e.g by capturing time(NULL) from immediately before and after the assignment and printing the time difference). You should have a decent idea of running time from that.
EDIT: time(NULL) is whole-second precision = bad and evil. Use clock(), as shown here, for the most accurate precision in the C/C++ standard library.
如果您使用的是 Visual Studio,下面的代码可以工作,但我不知道 gcc 的任何其他解决方案
if you are using visual studio the code down here would work, but idon't know about any other solutions for gcc