timeval 到字符串(两者之间转换)

发布于 2024-09-06 16:02:12 字数 230 浏览 8 评论 0原文

我试图将这两个组件从 timeval 结构中取出并将它们放入字符串中。

我在这方面运气不太好。我尝试先将其转换为 long,然后转换为字符串。我需要最有效的方法来做到这一点。

有什么想法吗?我不想首先转换为另一个数据结构(当地时间等)。我需要原始状态的秒和微秒。

编辑:我知道 stringstream 是这里的一个选项——我只是不确定它的效率如何。这里每一微秒都很重要,所以我正在寻找最快的实现。

I'm trying to pull the two components out of a timeval struct and place them into strings.

I'm not having much luck with this. I've attempted casting and converting first to a long and then to a string. I need the most efficient way to do this.

Any ideas? I do NOT want to convert to another data structure first (localtime, etc). I need the seconds and the microseconds in their original state.

EDIT: I know stringstream is an option here -- I'm just not sure how efficient that is. Every microsecond counts here, so I'm looking for the fastest implementation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

美煞众生 2024-09-13 16:02:12

Boost 的 lexical_cast 应该相当快。

编辑:
让我详细说明一下。以下是其用法示例:

std::string strSeconds = lexical_cast<std::string>(time.tv_sec);
std::string strMicroSec = lexical_cast<std::string>(time.tv_usec);

对于更复杂的字符串格式,Boost 文档建议使用基本的 std::stringstream。比如:

std::stringstream ss;
ss << time.tv_sec << " seconds, " << (time.tv_usec/1000L) << " milliseconds";
return ss.str();

相当快、可读、安全和标准。通过使用 cstdio 标头中的 sprintf ,您也许可以获得更快的速度。 (最好是 sprintf_s 如果可用) printf 中没有对 long 变量的明确支持,但现在在 32 位以上的计算机上它们通常具有相同的大小,因此您可以使用 %d 说明符来处理它们:

std::string tvtostr(timeval time) {
    // unless corrupted, the number of microseconds is always less than 1 second
    assert(time.tv_sec >= 0 && time.tv_usec >= 0 && time.tv_usec < 1000000000L);
    static_assert(sizeof(long)==4 && sizeof(int)==sizeof(long), 
        "assuming 32 bit ints and longs" ); 

    // space for one unbounded positive long, one long from 0 to 999,
    // the string literal below, and a '\0' string terminator
    boost::array<CHAR, 10+3+23+1> buffer; 

    sprintf_s(buffer.data(), buffer.size(), "%d seconds, %d milliseconds", 
        time.tv_sec, (time.tv_usec/1000L) );

    return buffer.data();
}

Boost's lexical_cast should be reasonably fast.

Edit:
Let me elaborate. Here's an example of its usage:

std::string strSeconds = lexical_cast<std::string>(time.tv_sec);
std::string strMicroSec = lexical_cast<std::string>(time.tv_usec);

For more complicated string formatting, the Boost documentation recommends the basic std::stringstream. Something like:

std::stringstream ss;
ss << time.tv_sec << " seconds, " << (time.tv_usec/1000L) << " milliseconds";
return ss.str();

Reasonably fast, readable, safe and standard. You might be able to get a little more speed by using sprintf from the cstdio header. (preferably sprintf_s if available) There's no explicit support for long variables in printf, but these days on 32-bit+ machines they're usually the same size so you can use the %d specifier to handle them:

std::string tvtostr(timeval time) {
    // unless corrupted, the number of microseconds is always less than 1 second
    assert(time.tv_sec >= 0 && time.tv_usec >= 0 && time.tv_usec < 1000000000L);
    static_assert(sizeof(long)==4 && sizeof(int)==sizeof(long), 
        "assuming 32 bit ints and longs" ); 

    // space for one unbounded positive long, one long from 0 to 999,
    // the string literal below, and a '\0' string terminator
    boost::array<CHAR, 10+3+23+1> buffer; 

    sprintf_s(buffer.data(), buffer.size(), "%d seconds, %d milliseconds", 
        time.tv_sec, (time.tv_usec/1000L) );

    return buffer.data();
}
向日葵 2024-09-13 16:02:12

除非我在这里遗漏了一些东西,否则您基本上希望以最快的方式将整数转换为字符串。
如果是这样,请查看 Matthew Wilson 在 DDJ 中的系列文章并进行选择:

灵活的 C++ #1:高效整数到字符串转换

灵活的 C++ #2:高效的整数到字符串转换

灵活的 C++ #3:高效的整数到字符串转换

灵活的 C++ #4:高效的整数到字符串转换

Unless I'm missing something here you basically want to convert integer to strings in the fastest possible way.
If that's so check out Matthew Wilson's series of articles in DDJ and take your pick:

Flexible C++ #1: Efficient Integer to String Conversions

Flexible C++ #2: Efficient Integer to String Conversions

Flexible C++ #3: Efficient Integer to String Conversions

Flexible C++ #4: Efficient Integer to String Conversions

不气馁 2024-09-13 16:02:12

除非您从分析中得到确凿的证据表明 std::ostringstream 太慢,否则我会使用它,因为它是最直接的方法。不要尝试过早地优化此类转化。

话虽这么说,对于微秒来说,如果您愿意放弃一块内存,您可以为该字段的每个可能值创建一个包含 1000000 个字符串的数组,并直接索引您需要的字符串。

Unless you have conclusive proof from profiling that std::ostringstream is too slow, I would use it as it's the most straightforward approach. Don't try to prematurely optimize these sorts of conversions.

That being said, for microseconds, if you're willing to give up a chunk of memory you can create an array of 1000000 strings for each possible value of that field, and index the string you need directly.

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