-1

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.

Kevin Whitefoot
  • 414
  • 3
  • 15
  • 9
    Have you measured it? [If you have two horses and you want to know which of the two is the faster then race your horses.](http://ericlippert.com/2012/12/17/performance-rant/) – Tim Schmelter Nov 28 '13 at 10:06
  • I doubt it because math transformations on floats are very dangerous. .NET would probably take a safe stance. Do the transform yourself, it will probably be faster. – usr Nov 28 '13 at 10:08
  • 3
    Like it or not, this **IS** an incredibly stupid question. .NET is not a real-time language. And unless "a lot" means "more than several billion" it won't matter one whit - and if it does mean that you have more problems than the speed of a little math. – Sam Axe Nov 28 '13 at 10:11
  • 3
    The question is not whether this transform should be applied in a particular case. The question is: does the compiler do it? Nothing stupid about it.; Also, you cannot just assume that the OP does not need the performance advantage of this. You are assuming it without evidence. – usr Nov 28 '13 at 10:16
  • Good point. I'll do that. But it doesn't really answer the question although it will give me enough information to decide what to do. – Kevin Whitefoot Nov 28 '13 at 10:21
  • Do you know how to look at what the compiler produces? – AakashM Nov 28 '13 at 10:32
  • 3
    "It doesn't matter unless you are doing it millions of times", is the old "premature optimization is evil". Of course people aren't supposed to make code less readable for little gain, but unless we know otherwise, why not just *assume* the code is in the inner loop of a video decoder? While there are probably other optimizations that are "better", I wouldn't call this question "incredibly stupid". Like Tim said you should probably update the question with a benchmark and add "WHY am I seeing these timings?" http://stackoverflow.com/questions/226465/should-i-use-multiplication-or-division – Anders Forsgren Nov 28 '13 at 10:38
  • Answered my own question with a simple benchmark. I also used ildasm to see what the IL looked like and it clearly uses div. But of course there is another compiler after that and I have no idea what the final machine code looks like. – Kevin Whitefoot Nov 28 '13 at 10:55
  • One more thing, in response to Dan-o: sometimes the question is not the only point either, it is the answer that is important. Part of my reason for asking the question was a desire to see it answered so that other people asking it would be able to find the question, an answer and the discussion about it. – Kevin Whitefoot Nov 28 '13 at 11:03
  • 1
    @KevinWhitefoot my old question [Is there a way to see the native code produced by theJITter for given C# / CIL?](http://stackoverflow.com/questions/1945719/is-there-a-way-to-see-the-native-code-produced-by-thejitter-for-given-c-sharp) might be of interest. – AakashM Nov 28 '13 at 14:29
  • @AakashM: Thanks, when I get time I will follow the method shown there and see wht happens. – Kevin Whitefoot Dec 02 '13 at 13:17
  • if x and y are integers then /4 will be much faster because you don't need to convert to and from doubles – phuclv Mar 21 '16 at 04:00

1 Answers1

6

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.

Rob
  • 5,223
  • 5
  • 41
  • 62
Kevin Whitefoot
  • 414
  • 3
  • 15