我做了一个小测试来检查全局函数/仿函数/lambda 作为 std::sort 函数的比较器参数的性能。 Functor 和 lambda 具有相同的性能。我惊讶地发现,看起来是最简单回调的全局函数却慢得多。
#include <stdafx.h>
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
const int vector_size = 100000;
bool CompareFunction(const string& s1, const string& s2)
{
return s1[0] < s2[0]; // I know that is crashes on empty string, but this is not the point here
}
struct CompareFunctor
{
bool operator() (const string& s1, const string& s2)
{
return s1[0] < s2[0];
}
} compareFunctor;
int main()
{
srand ((unsigned int)time(NULL));
vector<string> v(vector_size);
for(size_t i = 0; i < vector_size; ++i)
{
ostringstream s;
s << rand();
v[i] = s.str().c_str();
}
LARGE_INTEGER freq;
LARGE_INTEGER beginTime, endTime;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&beginTime);
// One of three following lines should be uncommented
sort(v.begin(), v.end(), CompareFunction);
// sort(v.begin(), v.end(), compareFunctor);
// sort(v.begin(), v.end(), [](const string& s1, const string& s2){return s1[0] < s2[0];});
QueryPerformanceCounter(&endTime);
float f = (endTime.QuadPart - beginTime.QuadPart) * 1000.0f/freq.QuadPart; // time in ms
cout << f << endl;
return 0;
}
一些特定于 Windows 的代码用于精确的执行时间测量。环境:Windows 7,Visual C++ 2010。当然,Release configuration with default optimizations opened。执行时间:
Global function 2.6 - 3.6 ms (???)
Functor - 1.7 - 2.4 ms
Lambda - 1.7 - 2.4 ms
那么,为什么全局函数比较慢? VC++ 编译器有问题,还是其他问题?
最佳答案
传递全局函数是最复杂的,而不是最简单的。
当你传入一个函数时,你实际上是在将一个指针传递给该函数,因此排序函数无法轻易地内联对该函数的调用,因为它在编译时不知道指针将指向什么。当然,它可能能够弄清楚通过函数指针的调用每次都调用相同的函数并将其全部内联,但这很难。
当您使用 lambda 或仿函数时,编译器在生成代码时确切地知道它需要调用哪个函数,因此很可能能够将其全部内联。
关于c++ - 传递给 std::sort 时,全局函数比仿函数或 lambda 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425528/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
我已经搜索过这个问题的答案,但没有成功,有一个类似的问题,但答案在这种情况下不起作用,它按数字项目排序。SimilarQuestion-Thatdidnotwork我正在尝试使用ruby的sort_by对一个项目进行降序排序和另一个升序排序。我只能找到一个。代码如下:#PrimarysortLastNameDescending,withtiesbrokenbysortingAreaofinterest.people=people.sort_by{|a|[a.last_name,a.area_interest]}任何指导肯定会有所帮助。示例数据:输入罗素,逻辑欧拉,图论伽罗瓦,抽象代