[英]How do I create a static library with GCC compiler optimization?
我有一个 function 来计算 C++ 中的点积。 我想用 -O3 编译器优化来编译这个 function。 我的代码库中的代码 Rest 是用 -O0 编译的。 为此,我创建了一个包含 function 的 static 库,并使用 -O3 编译了该库。 然后我将库链接到我的代码。 但我没有从我的库中得到优化。
测试.cpp
#include "config.h"
int multiply(uint128 *X1, uint128 *Y1, uint128 &ans, int input_length)
{
int i=0;
ans = 0;
if (input_length > 4)
{
for (; i < input_length - 4; i += 4)
{
ans += X1[i] * Y1[i];
ans += X1[i + 1] * Y1[i + 1];
ans += X1[i + 2] * Y1[i + 2];
ans += X1[i + 3] * Y1[i + 3];
}
}
for (; i < input_length; i++)
{
ans += X1[i] * Y1[i];
}
return 0;
}
int main()
{
int len = 500, wrapper = 50;
uint128 a[len], b[len], ans;
auto start = time_now, end = time_now;
long long ctr = 0;
for(int t = 0; t < wrapper; t++)
{
for(int i =0; i < len; i++)
{
a[i] = rand();
b[i] = rand();
}
start = time_now;
multiply(a, b, ans, len);
end = time_now;
ctr += std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
}
cout<<"time taken: "<<ctr<<endl;
}
Compilation:
g++ -O3 test.cpp -std=c++11
./a.out
time taken: 1372
优化的.hpp
#ifndef OPTIMIZED_HPP
#define OPTIMIZED_HPP
#include <bits/stdc++.h>
using namespace std;
typedef __uint128_t uint128;
int multiply(uint128 *X1, uint128 *Y1, uint128 &ans, int input_length);
#endif
主文件
#include "optimized.hpp"
typedef __uint128_t uint128;
#define time_now std::chrono::high_resolution_clock::now()
int main()
{
int len = 500, wrapper = 50;
uint128 a[len], b[len], ans;
auto start = time_now, end = time_now;
long long ctr = 0;
for(int t = 0; t < wrapper; t++)
{
for(int i =0; i < len; i++)
{
a[i] = rand();
b[i] = rand();
}
start = time_now;
multiply(a, b, ans, len);
end = time_now;
ctr += std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
}
cout<<"time taken: "<<ctr<<endl;
return 0;
}
Compilation:
(the name of the library file is optimized.cpp)
g++ -O3 -g -std=c++11 -c optimized.cpp
ar rcs libfast.a optimized.o
g++ main.cpp libfast.a -std=c++11 -O3
./a.out
time taken: 36140
恐怕你犯了 class 脚射:我优化了 function 我试图计时。 我们都做过一次。
您已经优化了test.cpp
程序-O3
。 编译器观察到程序忽略了multiply
的返回值。 它还具有multiply
的定义,并观察到主体没有外部影响,但忽略了返回值(在任何情况下都是常量0
)和忽略参考参数ans
。 所以这一行:
multiply(a, b, ans, len);
也可能是:
(void)0;
优化器剔除它。
您可以通过修改程序来纠正此问题,以便它使用multiply
的外部影响,以影响程序的 output 并且不能仅通过了解 function 的定义来预测。
但这还不够。 -O3
优化test.cpp
当编译器可以看到multiply
的定义时,与main.cpp
的相同优化相比,它仍然具有主要的信息优势,其中multiply
只是对黑盒的外部引用。
要有意义地测量使用-O3
优化的multiply
速度与使用-O0
优化的速度相比,您必须在其他条件相同的情况下测量每种情况。 所以至少这样做:
(无意中我的test.cpp
是你的main.cpp
)
$ g++ -Wall -Wextra -pedantic -c -O0 -o mult_O0.o optimized.cpp
$ g++ -Wall -Wextra -pedantic -c -O3 -o mult_O3.o optimized.cpp
$ g++ -Wall -Wextra -pedantic -c -O3 -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:13: warning: ISO C++ forbids variable length array ‘a’ [-Wvla]
13 | uint128 a[len], b[len], ans;
| ^
test.cpp:13:21: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
13 | uint128 a[len], b[len], ans;
| ^
(您可能会关心这些警告)
$ g++ -o tmult_O0 test.o mult_O0.o
$ g++ -o tmult_O3 test.o mult_O3.o
$ ./tmult_O0
time taken: 228461
$ ./tmult_O3
time taken: 99092
这表明-O3
正在做它的事情。
如果你这样做,你不需要确保在test.cpp
中使用multiply
的效果,因为现在编译器知道它无法知道黑盒multiply(a, b, ans, len)
并且不能剔除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.