结论
ubuntu x86平台qnx平台优化传值都是比传引用的差
但是差距很小
测试代码
#include <cstdint>
#include <ctime>
#include <string>#ifdef __linux__#define ITERATIONS 10000000
#else#define ITERATIONS 100000
#endiftemplate <typename... ARGS_TYPE>
void inno_log_value(const std::string &fmt_str, ARGS_TYPE... fmt_args) {printf(fmt_str.c_str(), fmt_args...);
}template <typename... ARGS_TYPE>
void inno_log_reference(const std::string &fmt_str, ARGS_TYPE const &... fmt_args) {printf(fmt_str.c_str(), fmt_args...);
}template <typename... ARGS_TYPE>
void inno_log_rvalue(const std::string &fmt_str, ARGS_TYPE &&... fmt_args) {printf(fmt_str.c_str(), std::forward<ARGS_TYPE>(fmt_args)...);
}int main() {// Test pass by valueuint64_t num64 = 1234567890;uint32_t num32 = 987654321;int32_t int32 = -12345;const char *str = "hello, world";char ch = 'A';float fl = 3.14159f;{clock_t startVal = clock();for (int i = 0; i < ITERATIONS; ++i) {inno_log_value("value: %lu, %u, %d, %s, %c, %f\n", num64, num32, int32, str, ch, fl);}clock_t endVal = clock();double elapsedVal = static_cast<double>(endVal - startVal) / CLOCKS_PER_SEC;printf("Pass by value: %f seconds\n", elapsedVal);}{// Test pass by referenceclock_t startRef = clock();for (int i = 0; i < ITERATIONS; ++i) {inno_log_reference("value: %lu, %u, %d, %s, %c, %f\n", num64, num32, int32, str, ch, fl);}clock_t endRef = clock();double elapsedRef = static_cast<double>(endRef - startRef) / CLOCKS_PER_SEC;printf("Pass by reference: %f seconds\n", elapsedRef);}{// Test pass by rvalue referenceclock_t startRValue = clock();for (int i = 0; i < ITERATIONS; ++i) {inno_log_rvalue("value: %lu, %u, %d, %s, %c, %f\n", num64, num32, int32, str, ch, fl);}clock_t endRValue = clock();double elapsedRValue = static_cast<double>(endRValue - startRValue) / CLOCKS_PER_SEC;printf("Pass by rvalue: %f seconds\n", elapsedRValue);}return 0;
}
ubuntu amd64 平台
编译: g++ -o test test.cpp -std=c++11 -O2
结果:
QNX 710
编译
/opt/qos222/host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.1.0-g++ -o test test.cpp -O2
结果: