修剪功能的性能

发布于 2024-10-04 19:09:00 字数 919 浏览 0 评论 0原文

我的旧修剪功能:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞琼窗梦回ˉ 2024-10-11 19:09:00

boost 的实现有可能比我相当简单的实现慢吗?

当然。

为了提高性能,我应该使用什么来代替 is_any_of 吗?

是的——你的原始代码。你没有说它有缺陷,也没有说你使用 boost 重新实现它的原因。如果原始代码中没有缺陷,那么就没有正当理由放弃原始实现。

将 Boost 引入代码库是有意义的。它带来了许多有用的功能。但仅仅为了使用新技术而放弃某个功能是一个很大的新手错误。

编辑:

回应您的评论:

我还是不明白,为什么boost的性能更差。

旨在为特定应用程序完成一项特定工作的手工函数通常比通用解决方案更快。 Boost 是一个很棒的通用工具库,可以节省大量编程工作并减少大量缺陷。但它很通用。您可能只需要以特定方式修剪琴弦,但 Boost 可以处理所有事情。这需要时间。

it possible that boost's implementation works slower than my quite straightforward implementation?

Of course.

Is there anything I should use instead of is_any_of in order to improve the performance?

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:

I still don't understand, why boost's performance is worse.

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.

夜司空 2024-10-11 19:09:00

为了回答您关于相对性能的问题,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 as strcspan), and these are very fast, however boost::algorithm::is_any_of uses (probably used to use, I'd hazard that in the later versions this has changed!) a std::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...

謌踐踏愛綪 2024-10-11 19:09:00

回答你关于相对表现的问题。

您正在使用 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 using boost::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文