在C中分析浮点使用情况 [英] Profiling floating point usage in C

查看:151
本文介绍了在C中分析浮点使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来计算一个标准的C代码实际执行的乘法的数量?我想到的代码基本上只是增加和乘法,它是主要利益的乘法,但它也不会受到其他操作的计数。


$ b $如果这是一个选项,我想我可以用'multiply(a,b)'代替'a * b',并为本地*操作符编写一个封面函数,b / c我真的不需要在这个测试中关心时间表现,但是主要的反对意见是要重新运行一堆源代码来运行测试。



我不反对重新编译源代码,可能是针对某些库或者含糊不清的(afaik)选项。 Valgrind想到了,但是如果我理解了valgrind的目的,那更多的是关于跟踪值而不是计算操作。解决方案

如果你的编译器支持软浮点(即使用整数实现的函数来模拟浮点),你可以编译你的程序在这种模式下(GCC中的 -msoft-float ),并使用你的最喜欢的分析工具来衡量它们被调用的次数。

许多处理器也有性能计数器可以计算浮点运算的次数已经退休了。根据硬件和操作系统的不同,您可能需要或可能不需要一些内核支持来利用它们。

Is there an easy way to count the number of multiplications actually executed by a piece of standard C code? The code I have in mind basically just does additions and multiplications, and it's the multiplications that are of primary interest, but it wouldn't hurt to get counts of the other operations as well.

If it were an option, I suppose I could go around replacing 'a * b' with 'multiply(a, b)' and write a cover function for the native * operator, b/c I really don't care about time performance during this test, but the primary objection to doing that is having to re-work a pile of source code just to run the test.

I have no objection to re-compiling the source, perhaps against some library or with obscure (afaik) options. Valgrind came to mind, but if I understand valgrind's purpose, that's more about tracing values than counting operations.

解决方案

If your compiler supports soft-float (i.e. using functions with integer implementations to emulate floating-point), you could compiler your program in that mode (-msoft-float in GCC), and use your favorite profiling tool to measure how many times they are invoked.

Many processors also have performance counters that can count the number of floating-point operations that have been retired. Depending on the hardware and OS, you may or may not need some amount of kernel support to take advantage of them.

这篇关于在C中分析浮点使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆