在VB.NET中是* 0.25快于/ 4 [英] Is *0.25 faster than / 4 in VB.NET
问题描述
我知道类似的问题已经得到回答,但是我找不到的答案都是针对.NET的。
x = y / 4
$ b b
:
x = y * 0.25
在任何人说不要担心之间的差别很小,我已经知道,但这将执行很多,选择一个在另一个在总执行时间上产生有用的差异,并且比更重要的重构操作更容易做。
也许我应该提及那些住在那里的人的利益一个完全自由的环境:我不能自由改变不同的语言。
这里建议的是一个简单的比较:
Dim y = 1234.567
Dim x As Double
Dim c = 10000000000.0
Dim ds作为日期
Dim df作为日期
ds =现在
对于i = 1到c
x = y / 4
接下来
df =现在
Console.WriteLine(divide&(df-ds).ToString)
ds =现在
对于i = 1到c
x = y * 0.25
$ b df = Now
Console.WriteLine(multiply&(df-ds).ToString)
输出为:
divide 00:00:52.7452740
multiply 00:00:47.2607256
所以divide看起来慢了大约10%。但这个差别是如此之小,我怀疑它是偶然的。另外两个运行结果:
divide 00:00:45.1280000
multiply 00:00:45.9540000
divide 00:00:45.9895985
multiply 00:00:46.8426838
建议实际上进行了优化或者算术运算是总时间的一小部分。
在任何一种情况下,这意味着我不需要关心
事实上,ildasm显示IL在第一个循环中使用div,在第二个循环中使用mul。所以它不会使取消毕业。除非JIT编译器。
I know similar questions have been answered before but none of the answers I could find were specific to .NET.
Does the VB.NET compiler optimize expressions like:
x = y / 4
by compiling:
x = y * 0.25
And before anyone says don't worry the difference is small, i already know that but this will be executed a lot and choosing one over the other could make a useful difference in total execution time and will be much easier to do than a more major refactoring exercise.
Perhaps I should have mentioned for the benefit of those who live in an environment of total freedom: I am not at liberty to change to a different language. If I were I would probably have written this code in Fortran.
As suggested here is a simple comparison:
Dim y = 1234.567
Dim x As Double
Dim c = 10000000000.0
Dim ds As Date
Dim df As Date
ds = Now
For i = 1 To c
x = y / 4
Next
df = Now
Console.WriteLine("divide " & (df - ds).ToString)
ds = Now
For i = 1 To c
x = y * 0.25
Next
df = Now
Console.WriteLine("multiply " & (df - ds).ToString)
The output is:
divide 00:00:52.7452740
multiply 00:00:47.2607256
So divide does appear to be slower by about 10%. But this difference is so small that I suspected it to be accidental. Another two runs give:
divide 00:00:45.1280000
multiply 00:00:45.9540000
divide 00:00:45.9895985
multiply 00:00:46.8426838
Suggesting that in fact the optimization is made or that the arithmetic operations are a vanishingly small part of the total time.
In either case it means that I don't need to care which is used.
In fact ildasm shows that the IL uses div in the first loop and mul in the second. So it doesn't make the subsitution after all. Unless the JIT compiler does.
这篇关于在VB.NET中是* 0.25快于/ 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!