0

Division Vs Multiplication Equivalent

Spread the love

Division Vs Multiplication Equivalent

It’s a common belief in the world of programming that division is slower than multiplication. Not in order of magnitudes, but relatively. Granted, it depends on the underlying architecture, but generally speaking, a computer will be able to calculate the product of two numbers faster than when dividing.

 

Well, you guessed it, I came across quite a bit of code that had simple fractional division embedded within it:
 

 

That’s where this Curious Consultant started wondering… is there a C# performance impact using division vs multiplication equivalent?

 

That is, if the code was rewritten as follows:

Let’s find out!

 

The test is pretty straight forward

The code is written in Visual Studio 2017 targeting .Net Framework version 4.8 x64, compiled in “Release” mode the build option “Optimize Code” checked. The source code is available at the end of this blog so you can benchmark it on your own system.

 

Assumptions:

  • No exception handling as no exceptions should occur
  • Only fractional variants with “1/” are used. Otherwise there are more operations involved. Eg, 2/3 means multiple x by 2 and then divide by 3.
  • to make the test worth-while, several fractional variants were used: 1/2, 1/3, 1/4, 1/5, 1/8, 1/100, 1/1000

 

The exe file was run under Windows 10 Professional 64-bit with 32GB of memory.

 

The test was run with each fraction and its equivalent the following number of times:

  • 10,000
  • 1,000,000
  • 100,000,000

Totals are summed at the end of each run to verify the same number of operations were performed.
 

Are you a betting person?

Before starting, I predicted that the multiplication equivalent would have a winning streak in the results. Whether it won by a little or a lot didn’t bother me so much as that it won consistently.

 

This test was run 3 times, but only the last set of results are being shown below as the results were equivalent for each run.

 

All times are indicated in minutes.seconds.milliseconds format. Lower numbers indicate faster run time performance.

 

Winners are highlighted in green; there are no points for second place.

 

 

Number of Loops

 

10,000

1,000,000

100,000,000

1 / 2

00:00.0000307

00:00.0039268

00:00.3399387

0.5

00:00.0000303

00:00.0033720

00:00.3642056

 

1 / 3

00:00.0000307

00:00.0035239

00:00.3522421

0.3333

00:00.0000348

00:00.0030266

00:00.3392868

 

1 / 4

00:00.0000324

00:00.0029968

00:00.3369088

0.25

00:00.0000319

00:00.0041509

00:00.3342529

 

1 / 5

00:00.0001036

00:00.0042574

00:00.3432200

0.2

00:00.0000371

00:00.0038947

00:00.3395073

 

1 / 8

00:00.0000339

00:00.0034149

00:00.3196951

0.125

00:00.0000472

00:00.0033856

00:00.3171413

 

1 / 100

00:00.0001030

00:00.0032920

00:00.3273697

0.01

00:00.0000300

00:00.0037606

00:00.3185416

 

1 / 1000

00:00.0000332

00:00.0034861

00:00.3193875

0.001

00:00.0000703

00:00.0028892

00:00.3158620

 

In Summary:

The prediction is on track. In C#, when deciding to use division vs multiplication equivalent, the multiplication equivalent won 15/21 times. In the other two runs, it was also 15/21 and 16/21.

 

There’s no distinct pattern or threshold that appears in the results of when to use one versus the other although overall multiplication is the winner.

 

And when it did win, with few exceptions, it wasn’t by leaps in bounds.

 

So in the more practical world, it comes back down to whatever is easiest for reading/writing code.

 

But if you have the need for speed, definitely use the multiplication equivalent where possible.

 

The Code:

 


Spread the love

David Lozinski

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments