有效地将 unsigned Short 转换为 char*
将 unsigned Short 转换为 char* (即将 25 转换为 '25')的有效、可移植的方法是什么?
我想避免诸如获取 (std::string) 字符串之类的事情。在这种情况下,性能很重要,因为这种转换需要快速且频繁地进行。
我正在研究诸如使用 sprintf 之类的事情,但想探索任何和所有的想法。
What would be an efficient, portable way to convert a unsigned short to a char* (i.e. convert 25 to '25').
I'd like to avoid things such as getting (std::string) strings involved. Performance is important in this case since this conversion will need to happen quickly and often.
I was looking into things such as using sprintf but would like to explorer any and all ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,正确地做,然后快速地做——只有当您可以确定地看到一段代码性能不佳时才进行优化。
snprintf()
放入缓冲区将执行您想要的操作。这是最快的解决方案吗?一点也不。但它是最简单的之一,足以让您的代码进入工作状态。从那里开始,如果您发现对snprintf()
的调用非常费力,需要优化,那么只有这样才能寻求更快的解决方案。First off, do it right, then do it fast--only optimize if you can see for certain that a piece of code is not performant.
snprintf()
into a buffer will do what you want. Is it the fastest possible solution? Not at all. But it is among the simplest, and it will suffice to get your code into a working state. From there, if you see that those calls tosnprintf()
are so laborious that they need to be optimized, then and only then seek out a faster solution.一个字符串数组,
也许?您可以编写一个小程序来非常轻松地生成表源代码,然后在您的项目中使用该文件。
编辑:我不明白你不想让字符串参与其中的意思。
An array of strings such that
maybe? You could write a small program that generates the table source code for you quite easily, and then use this file in your project.
Edit: I don't get what you mean by you don't want to ge strings involved.
试试这个:
try this:
我想说至少尝试 sprintf 并且由于您已将其标记为 C++,请尝试 StringStream ,并实际分析它们。在许多情况下,编译器足够聪明,可以构建运行良好的东西。只有当您知道这将成为瓶颈时,您才需要真正找到更快的方法。
I would say at least try sprintf and since you have this tagged as C++, try StringStream, and actually profile them. In many cases the compiler is smart enough to build something that works pretty well. Only when you know it's going to be a bottleneck do you need to actually find a faster way.
我在这里对各种功能进行了测试,这就是我得出的结果:
write_ushort: 7.81 s
uShortToStr:8.16 秒
转换:6.71 秒
use_sprintf: 49.66 s
(Write_ushort 是我的版本,我试图尽可能清楚地编写它,而不是进行微优化,以格式化为给定的字符缓冲区;use_sprintf 是明显的 sprintf(buf, "%d", x) 和没有别的;其他两个是从这里的其他答案中获取的。)
它们之间有一个非常惊人的区别,不是吗?面对几乎一个数量级的差异,谁会想到使用 sprintf 呢?哦,是的,我迭代了每个测试函数多少次?
Sprintf 转换了整个可能范围的无符号短裤,然后在我大约 5 年的旧笔记本电脑上再次执行整个范围 2,999 次,平均每次转换大约 0.25 µs。
Sprintf 是可移植的;它是否也足够高效满足您的要求?
我的版本:
编译时日志 TMP 函数并不是什么新鲜事,但包括这个完整的示例,因为它是我使用的:
I hacked together a test of various functions here, and this is what I came up with:
write_ushort: 7.81 s
uShortToStr: 8.16 s
convert: 6.71 s
use_sprintf: 49.66 s
(Write_ushort is my version, which I tried to write as clearly as possible, rather than micro-optimize, to format into a given character buffer; use_sprintf is the obvious sprintf(buf, "%d", x) and nothing else; the other two are taken from other answers here.)
This is a pretty amazing difference between them, isn't it? Who would ever think to use sprintf faced with almost an order of magnitude difference? Oh, yeah, how many times did I iterate each tested function?
Sprintf converted the entire possible range of unsigned shorts, then did the whole range again 2,999 more times at about 0.25 µs per conversion, on average, on my ~5 year old laptop.
Sprintf is portable; is it also efficient enough for your requirements?
My version:
Compile-time log TMP functions are nothing new, but including this complete example because it's what I used: