我正在读一本书,作者说这if( a < 901 )
比书更快if( a <= 900 )
.
与此简单示例不完全相同,但循环复杂代码略有性能变化.我想这必须对生成的机器代码做一些事情,以防它甚至是真的.
我需要检查C#中的项目列表中是否存在项目,所以我有这一行:
if (!myList.Any(c => c.id == myID)))
Run Code Online (Sandbox Code Playgroud)
Resharper建议我将其更改为:
if (myList.All(c => c.id != myID)))
Run Code Online (Sandbox Code Playgroud)
我可以看出它们是等价的,但为什么它表明这种变化呢?由于某种原因,第一个实现是否较慢?
如果要比较两个整数,运算符是否会影响执行比较所需的时间?例如,给定:
if (x < 60)
Run Code Online (Sandbox Code Playgroud)
和
if (x <= 59)
Run Code Online (Sandbox Code Playgroud)
哪个会提供最佳性能,还是性能差异可以忽略不计?性能结果是否取决于语言?
我经常发现自己在代码中混合使用这些运算符.任何想法将不胜感激.
我在存储有关图像信息的数组上运行图像分析代码.不幸的是,代码非常繁重,平均需要25秒来运行单帧.我看到的主要问题是数组寻址.哪个是运行2d阵列最快的,并且完全没有任何差异
水平然后垂直
for (int y = 0; y < array.Length; ++y)
for (int x = 0; x < array[].Length; ++x)
//Code using array[y][x]
Run Code Online (Sandbox Code Playgroud)
和垂直然后horrizontal?
for (int x = 0; x < array[].Length; ++x)
for (int y = 0; y < array.Length; ++y)
//Code using array[y][x]
Run Code Online (Sandbox Code Playgroud)
此外,我试图避免直接寻址和使用指针.
for (int y = 0; y < array.Length; ++y)
int* ptrArray = (int*)array[0];
for (int x = 0; x < array[].Length; ++x, ++ptrArray)
//Code using ptrArray for array[y][x]
Run Code Online (Sandbox Code Playgroud)
要么
for (int x = 0; …
Run Code Online (Sandbox Code Playgroud) 我有一份清单.有什么区别list.Count > 0
和list.Count != 0
?或者这些代码中的任何性能差异?
if (list.Count > 0)
// do some stuff
if (list.Count != 0)
// do some stuff
Run Code Online (Sandbox Code Playgroud)
注意:
list.Count
不能少于ziro ..
我有一位同事正在与我争辩说,在if语句中使用时,否定条件更快.我已经争辩说编译器正在优化代码,我们真的不知道这个条件将如何实际结束.
例如,她声称这样做
if(!MyCondition)
Run Code Online (Sandbox Code Playgroud)
比...更快
if(MyCondition)
Run Code Online (Sandbox Code Playgroud)
我搜索过,无法在任何地方找到参考资料.所以,我的问题是:
在if语句中使用条件时,是否真的有提高性能的偏好?或者只是提高可读性?
哪个运营商更快:>
或==
?
示例:我想测试一个值(可以有一个正值或-1)对-1:
if(time > -1)
// or
if (time != -1)
Run Code Online (Sandbox Code Playgroud)
时间有类型"int"
c# ×4
performance ×4
c++ ×3
arrays ×1
assembly ×1
c ×1
compare ×1
comparison ×1
if-statement ×1
linq ×1
operators ×1
pointers ×1
resharper ×1