将包含ascii字符串的byte[]快速转换为int/double/date等,无需new String
我得到 FIX 消息字符串 (ASCII) 作为 ByteBuffer。我解析标签值对并将值存储为树形图中的原始对象,并以标签作为键。所以我需要根据其类型将 byte[] 值转换为 int/double/date 等。
最简单的方法是创建新的字符串并将其传递给标准转换器函数。例如,
int convertToInt(byte[] buffer, int offset, int length)
{
String valueStr = new String(buffer, offset, length);
return Integer.parseInt(valueStr);
}
我知道在 Java 中,创建新对象非常便宜,仍然有任何方法可以直接将此 ascii byte[] 转换为原始类型。我尝试了手写函数来执行此操作,但发现它非常耗时并且没有带来更好的性能。
是否有任何第三方库可以这样做,最重要的是它值得这样做吗?
I get FIX message string (ASCII) as ByteBuffer. I parse tag value pairs and store values as primitive objects in the treemap with tag as key. So I need to convert byte[] value to int/double/date, etc. depending on its type.
Simplest way is to create new String and pass it to standard converter functions. e.g.
int convertToInt(byte[] buffer, int offset, int length)
{
String valueStr = new String(buffer, offset, length);
return Integer.parseInt(valueStr);
}
I understand that in Java, creating new object is very inexpensive, still is there any way to convert this ascii byte[] to primitive type directly. I tried hand written functions to do so, but found it to be time consuming and didn't result in better performance.
Are there any third party libraries for doing so and most of all is it worth doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几乎肯定不是 - 在采取重大措施缓解它之前,您应该采取措施检查这是否是一个性能瓶颈。
你现在的表现怎么样?它需要是什么? (“尽可能快”不是一个好的目标,否则你永远不会停止 - 弄清楚什么时候你可以说你“完成”了。)
分析代码 - 真正的问题是在字符串创建中?检查垃圾收集等的频率(再次使用分析器)。
每种解析类型可能具有不同的特征。例如,对于解析整数,如果您发现在很长一段时间内您得到的是单个数字,您可能想要特殊情况:
...但是检查这种情况的频率发生在你走那条路之前。对实际上从未出现过的特定优化进行大量检查会适得其反。
Almost certainly not - and you should measure to check that this is a performance bottleneck before going to significant effort to alleviate it.
What's your performance like now? What does it need to be? ("As fast as possible" isn't a good goal, or you'll never stop - work out when you can say you're "done".)
Profile the code - is the problem really in string creation? Check how often you're garbage-collecting etc (again, with a profiler).
Each parsing type is likely to have different characteristics. For example, for parsing integers, if you find that for a significant amount of the time you've got a single digit, you might want to special-case that:
... but check how often this occurs before you go down that path. Applying lots of checks for particular optimizations that never actually crop up is counter-productive.
同意 Jon 的观点,但是在处理许多 FIX 消息时,这种情况很快就会增加。
下面的方法将允许使用空格填充数字。如果您需要处理小数,那么代码会略有不同。两种方法之间的速度差异为 11 倍。ConvertToLong 导致 0 次 GC。下面的代码是c#中的:
Agree with Jon however when processing many FIX messages this quickly adds up.
The method below will allow for space padded numbers. If you need to handle decimals then code would be slightly different. Speed difference between the two methods is a factor of 11. The ConvertToLong results in 0 GCs. Code below is in c#:
看一下 ByteBuffer。它具有执行此操作的功能,包括处理字节顺序(字节顺序)。
Take a look at ByteBuffer. It has capabilities for doing this, including dealing with byte order (endianness).
一般来说,我不喜欢粘贴这样的代码,但无论如何,100 行是如何完成的(生产代码)
我不建议使用它,但有一些参考代码很好(通常)
Generally I have no preference to paste such code but anyways, 100 lines how it's done (production code)
I'd not advise using it but having some reference code it's nice (usually)