C++ c++;如果将'map::key_comp'作为一种将数字与现有密钥进行比较的方法调用,会影响性能吗?
我有一个定义如下的类型C++ c++;如果将'map::key_comp'作为一种将数字与现有密钥进行比较的方法调用,会影响性能吗?,c++,dictionary,c++11,functional-programming,C++,Dictionary,C++11,Functional Programming,我有一个定义如下的类型 using BookMapT = std::map<long long int, long long int, std::function<bool(long long int,long long int)>>; BookMapT greater_map(greater<long long int>{}); BookMapT less_map(less<long long int>{}); 从我可以看到key\u comp
using BookMapT = std::map<long long int, long long int, std::function<bool(long long int,long long int)>>;
BookMapT greater_map(greater<long long int>{});
BookMapT less_map(less<long long int>{});
从我可以看到key\u comp
返回比较对象的copy
,这copy
操作是否会影响性能?
最让我困惑的是复制std::function
要花多少钱
谢谢。\include
#include <chrono>
#include <functional>
#include <iostream>
#include <random>
typedef std::function<bool(long long int,long long int)> TF;
TF cp(const TF & f) { return f; }
int cp1(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += cp(f)(i, n);
}
return n;
}
int cp2(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += TF(f)(i, n);
}
return n;
}
int no_cp(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += f(i, n);
}
return n;
}
typedef int(*TP)(TF, int, int);
void test(TP p, int n)
{
double s = 0;
int c = 0;
for(int i = 10; --i >= 0;) {;
auto t = std::chrono::steady_clock::now();
c += (*p)(std::less<long long int>{}, 50000000, n);
std::chrono::duration<double> d = std::chrono::steady_clock::now() - t;
s += d.count();
}
std::cout << s / 10 << "(" << c << ")" << std::endl;
}
int main() {
int n = static_cast<int>(std::random_device()());
std::cout << "cp1: ";
test(&cp1, n);
std::cout << "cp2: ";
test(&cp2, n);
std::cout << "no_cp: ";
test(&no_cp, n);
}
#包括
#包括
#包括
typedef std::函数TF;
TF cp(const TF&f){return f;}
整数cp1(tff,整数倍,整数n){
int c=0;
对于(int i=times;--i>=0;){
c+=cp(f)(i,n);
}
返回n;
}
整数cp2(tff,整数倍,整数n){
int c=0;
对于(int i=times;--i>=0;){
c+=TF(f)(i,n);
}
返回n;
}
int no_cp(tff,int次,int n){
int c=0;
对于(int i=times;--i>=0;){
c+=f(i,n);
}
返回n;
}
类型定义内部(*TP)(TF,内部,内部);
无效试验(TP p,int n)
{
双s=0;
int c=0;
对于(inti=10;--i>=0;){;
自动t=std::chrono::稳定时钟::现在();
c+=(*p)(标准:小于{},50000000,n);
std::chrono::duration d=std::chrono::stable_clock::now()-t;
s+=d.count();
}
std::cout您正在使用的两个比较器是无状态的,没有数据被复制,因此复制它们的开销可以忽略不计。另一方面,如果您使用long-long-int
的别名,例如std::int64\u-t
或至少是使用
的自定义,则显示的代码的读取和管理将更加干净。
#include <chrono>
#include <functional>
#include <iostream>
#include <random>
typedef std::function<bool(long long int,long long int)> TF;
TF cp(const TF & f) { return f; }
int cp1(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += cp(f)(i, n);
}
return n;
}
int cp2(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += TF(f)(i, n);
}
return n;
}
int no_cp(TF f, int times, int n) {
int c = 0;
for(int i = times; --i >= 0;) {
c += f(i, n);
}
return n;
}
typedef int(*TP)(TF, int, int);
void test(TP p, int n)
{
double s = 0;
int c = 0;
for(int i = 10; --i >= 0;) {;
auto t = std::chrono::steady_clock::now();
c += (*p)(std::less<long long int>{}, 50000000, n);
std::chrono::duration<double> d = std::chrono::steady_clock::now() - t;
s += d.count();
}
std::cout << s / 10 << "(" << c << ")" << std::endl;
}
int main() {
int n = static_cast<int>(std::random_device()());
std::cout << "cp1: ";
test(&cp1, n);
std::cout << "cp2: ";
test(&cp2, n);
std::cout << "no_cp: ";
test(&no_cp, n);
}