[英]Performance c++: Dynamic loading library and loops
I want to make fast iterations of a dynamically loaded library symbol. 我想对动态加载的库符号进行快速迭代。 To see how much performance I can get comparing to hard-coding the function, I did some benchmarks for a simple addition operation. 为了查看与硬编码该函数相比可以获得多少性能,我对一个简单的加法运算做了一些基准测试。
I use this function to do the operation 我用这个功能做手术
long int func(long int x)
{
return x+1;
}
And this loops to test the time 这循环测试时间
Hard-coded: 硬编码:
for(i=0;i<N;i++)
c = c + 1;
With the function (defined in the same file where the loop is): 使用函数(在循环所在的同一文件中定义):
for(i=0;i<N;i++)
c = func(c);
With func from a linked object (func.o): 使用链接对象中的func(func.o):
for(i=0;i<N;i++)
c = object_func(c);
With func from a dynamically loaded symbol ( using dlopen and dlsym ) 使用来自动态加载的符号的func(使用dlopen和dlsym)
for(i=0;i<N;i++)
c = dynamic_func(c);
This are the results for different N values, and everything compiled with g++ -Ofast: 这是不同N值的结果,以及使用g ++ -Ofast编译的所有结果:
There are a couple of ''mysterious'' things, first the function version give some 0 time values so there are missing points in its line. 这里有一些“神秘”的事情,首先,函数版本给出了0个时间值,因此其行中缺少点。 Then, is almost an order of magnitude less than the hard-coded version. 然后,几乎比硬编码版本小一个数量级。 I presume that the compiler is optimizing the operation, so the times don't rise. 我认为编译器正在优化操作,因此时间不会增加。 Other strange thing is that for small N values the object or shared libraries give better times. 另一个奇怪的事情是,对于较小的N值,对象或共享库会提供更好的时间。
This is the same without optimizations. 没有优化就一样。 Again is curious that the hard-coded starts slower than the rest, but now it is clear that the optimizer was responsible of the flat curves. 再次感到奇怪的是,硬编码的启动速度比其他编码慢,但是现在很明显,优化器负责平坦曲线。 Nonetheless, the N parameter is an argument to the program, it is not fixed at building time. 但是,N参数是程序的参数,在构建时并不固定。
Anyone can explain this behavior? 任何人都可以解释这种行为吗? And the main question: Is there a way to get better results for object or dynamic libraries for large N? 还有一个主要问题:对于大N的对象或动态库,是否有办法获得更好的结果?
Thanks 谢谢
Nothing misterious, it's simple: 没什么大不了的,很简单:
object_func, dynamic_func are the same thing except dinamic requires bit more time for library loading. object_func,dynamic_func相同,只是dinamic需要更多的时间来加载库。 Complexity of cycle is O(n), so the charts. 循环的复杂度是O(n),所以图表。
c++ is not just c with classes it also has wary powerfull optimizer. c ++不仅是带有类的c语言,它还具有强大的功能强大的优化器。 So the optimizer look into hard code and replace it with this: 因此,优化器将研究硬代码并用以下代码替换:
c = N; c = N;
Note: optimizer only can do that if it knows N and operation/function in compile time. 注意:优化器只有在编译时知道N和操作/函数时才能这样做。
Behaviour explanation : 行为说明:
For the first two, the code is optimized out by the compiler (O(1)). 对于前两个,代码由编译器(O(1))优化。 For the last two, the function is obviously called (O(N)). 对于最后两个,该函数显然称为(O(N))。
How to get better results : 如何获得更好的结果:
You should pass the N value as a parameter to your dynamically loaded function and perform the loop inside the function to avoid the function call overhead on large N. 您应该将N值作为参数传递给动态加载的函数,并在函数内部执行循环,以避免在大N上调用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.