修剪功能的性能
我的旧修剪功能:
string TailTrimString (const string & sSource, const char *chars) {
size_t End = sSource.find_last_not_of(chars);
if (End == string::npos) {
// only "*chars"
return "";
}
if (End == sSource.size() - 1) {
// noting to trim
return sSource;
}
return sSource.substr(0, End + 1);
}
我决定使用 boost 来代替它,并编写了简单的:
string TailTrimString (const string & sSource, const char *chars) {
return boost::algorithm::trim_right_copy_if(sSource,boost::algorithm::is_any_of(chars));
}
我惊讶地发现新功能的运行速度要慢得多。 我做了一些分析,发现函数 is_any_of 非常慢。
boost 的实现是否有可能比我相当简单的实现慢?为了提高性能,我应该使用什么来代替 is_any_of
吗?
我还在 boost 的邮件列表中发现了关于此问题的讨论 ,但我仍然不确定如何提高代码的性能。
我使用的 boost 版本是 1.38,这是相当旧的,但我想从那时起这段代码并没有改变太多。
谢谢。
My old Trimming function:
string TailTrimString (const string & sSource, const char *chars) {
size_t End = sSource.find_last_not_of(chars);
if (End == string::npos) {
// only "*chars"
return "";
}
if (End == sSource.size() - 1) {
// noting to trim
return sSource;
}
return sSource.substr(0, End + 1);
}
Instead of it I've decided to use boost, and wrote the trivial:
string TailTrimString (const string & sSource, const char *chars) {
return boost::algorithm::trim_right_copy_if(sSource,boost::algorithm::is_any_of(chars));
}
And I was amazed to find out that the new function works much slower.
I've done some profiling, and I see that the function is_any_of
is very slow.
Is it possible that boost's implementation works slower than my quite straightforward implementation? Is there anything I should use instead of is_any_of
in order to improve the performance?
I also found a discussion on this matter in the boost's mailing list, but I am still not sure on how can I improve the performance of my code.
The boost version that I use is 1.38, which is quite old, but I guess this code didn't change too much since then.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。
是的——你的原始代码。你没有说它有缺陷,也没有说你使用 boost 重新实现它的原因。如果原始代码中没有缺陷,那么就没有正当理由放弃原始实现。
将 Boost 引入代码库是有意义的。它带来了许多有用的功能。但仅仅为了使用新技术而放弃某个功能是一个很大的新手错误。
编辑:
回应您的评论:
旨在为特定应用程序完成一项特定工作的手工函数通常比通用解决方案更快。 Boost 是一个很棒的通用工具库,可以节省大量编程工作并减少大量缺陷。但它很通用。您可能只需要以特定方式修剪琴弦,但 Boost 可以处理所有事情。这需要时间。
Of course.
Yeah -- your original code. You said nothing about it having a defect, or the reason why you re-implemented it using boost. If there was no defect in the original code, then there was no valid reason to chuck the original implementation.
Introducing Boost to a codebase makes sense. It brings a lot of functionality that can be helpful. But gutting a function for the sole purpose of using a new technology is a big rookie mistake.
EDIT:
In response to your comment:
A hand-crafted function that is designed to do one specific job for one specific application will often be faster than a generic solution. Boost is a great library of generic tools that can save a lot of programming and a lot of defects. But its generic. You may only need to trim your string in a specific way, but Boost handles everything. That takes time.
为了回答您关于相对性能的问题,
std::string::find_last_not_of
将包装 C 字符串例程(例如strcspan
),并且这些例程非常快,但是boost::algorithm::is_any_of
使用(可能曾经使用过,我敢说在以后的版本中这已经改变了!)一个std::set
为要查找的字符集并在该集中检查每个字符 - 这不会那么快!编辑:只是添加一个回显,你的函数可以工作,它没有损坏,也不慢,所以不用费心去改变它......
In answer to your question about the relative performance, the
std::string::find_last_not_of
, will wrap C string routines (such asstrcspan
), and these are very fast, howeverboost::algorithm::is_any_of
uses (probably used to use, I'd hazard that in the later versions this has changed!) astd::set
for the set of characters to look for and does a check in this set for each character - which will not be anywhere near as fast!EDIT: just to add an echo, your function works, it's not broken, it's not slow, so don't bother changing it...
回答你关于相对表现的问题。
您正在使用 boost::algorithm::trim_right_copy_if ,它根据名称在修剪之前创建输入的副本。尝试使用
boost::algorithm::trim_right_if
看看是否具有更好的性能。该函数将就地执行操作,而不是在新字符串上执行操作。In answer to your question about the relative performance.
You are using
boost::algorithm::trim_right_copy_if
which, according to the name, creates a copy of the input before trimming. Try usingboost::algorithm::trim_right_if
to see if that has better performance. This function will perform the operation in-place instead of on a new string.