- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个高性能解析器,在我看来 Int32.Parse
可能太慢了。我编写了一个假设输入正确的简单版本,并且它的性能要好得多。那么我应该创建自己的版本吗?或者是否有另一种更快的方法可用?
我的方法是这样的:
// parse simple int, assuming relatively correct input (i.e. all digits)
public static int ParseInt32Simply(string str) {
if (str == null) throw new ArgumentNullException("str");
if (str.Length == 0) throw new ArgumentException("str is empty");
int sign = 1, index = 0;
if (str[0] == '-') { sign = -1; index = 1; }
else if (str[0] == '+') { index = 1; }
int result = 0;
for (; index < str.Length; ++index) {
result = 10 * result + (str[index] - '0');
}
if (result < 0) throw new OverflowException(str + " is too large for Int32");
return result * sign;
}
我的结果与内置的等效结果非常不同:
Int32.Parse took 8.2775453 seconds
ParseInt32Simply took 0.6511523 seconds
Int32.Parse took 6.7625807 seconds
ParseInt32Simply took 0.4677390 seconds
(在我的机器上运行 2500 万次迭代;P4 3 GHz,运行 VS 2008 SP1)
那么,我应该使用我的版本吗?或者我可以使用其他方法吗?
最佳答案
您是否分析过您的代码以确定 ParseInt32 实际上是瓶颈?我不会替换您正在编写代码的环境的“标准库”的一部分,除非您确定您会看到好处。
关于.net - 我应该推出我自己的 ParseInt32 版本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/416182/
来自 documentation : This method behaves identically to the global function parseInt() 但是,由于它是实验性的,the
这个问题在这里已经有了答案: Javascript parseInt() with leading zeros (7 个答案) 关闭 8 年前。 在IE8中发现了一件奇怪的事情: parseInt(
我对 parseInt 的特定结果感到困惑。我以为我了解它的大部分怪癖,但这一个还不清楚。 parseInt("010", 10); // 10 parseInt(010, 10); // 8,
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: JavaScript function parseInt() doesn't parse numbers w
这个问题纯属好奇。 通过控制台 parseInt(1111111111111111,2) // 16 1's 返回 65535 和 parseInt(11111111111111111,2) // 1
我正在开发一个 Java 项目,将每个整数添加到下一行中的整数,直到文件中没有任何行可供读取。因此,为了能够添加它,我必须在该行中使用 Integer.parseInt(...) 然后添加它。P.S:
在 MDN 的 Number.parseInt部分,它说: The Number.parseInt() method parses a string argument and returns an i
为什么 parseInt('60', 10) 与 parseInt('60', 0) 相同? JavaScript 将 0 基数转换成什么? 最佳答案 来自 MDN ( https://develop
在阅读 JavaScript 中的 NaN 时,我遇到了一些示例。然后我开始进行更多测试,结果让我感到惊讶。例如,我想知道为什么 JavaScript 对下面两个几乎相同的语句的解释不同 parseI
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 为什么 parseInt
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我认为它与八进制
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我正在研究 jav
我正在阅读 this但我对 parseInt 中带有基数参数一章的内容感到困惑 为什么是 parseInt(8, 3) → NaN 和 parseInt(16, 3) → 1? AFAIK 8 和 1
我必须构建一个由我构建的一些结构组成的数组: 类(Item)的构建方式如下:(如下所示) 现在我的问题是我试图将从另一个地方(在本例中为列表)获取的一些字符串数字解析为 int。 但我收到此错误: T
我是 Javascript 新手,有一个关于使用 parseInt() 函数将字符串转换为数字的问题。 我通过编写以下代码来练习 parseInt,并在将用户输入从字符串变为数字之前使用 typeof
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我正在解析一个字
我有这个 jQuery 片段,用于从输入字段获取 ID 号 $('table th input').change(function() { var id = $(this).attr('id'
我有这个 JavaScript 文件(Rhino 1.7R4)。 importPackage(java.io); importPackage(java.lang); importPackage(jav
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
当我尝试使用 parseInt(value) 将其转换为 int 时,我有 var value="10+10" ,它给了我 NaN。如果字符串中有特殊字符,他们是否可以选择转换字符串?结果应为 20
我是一名优秀的程序员,十分优秀!