Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing speed acting strange

I got this simple question which confused me a bit. I got 2 processors Both of which can individually do 1 billion operations in 33.0360723. Yet both of them together do the operations in 27.4996964.

This makes no sense for me, if the time for a task for one processor is X, then should it not be X/2 for both of them together?

My code:

Function calc(ByVal i As Integer, ByVal result As String)
    Math.Sqrt(i)
    Return True

End Function
Sub Main()
    Dim result As String = Nothing


    Dim starttime As TimeSpan
    starttime = DateTime.Now.TimeOfDay
    For i = 0 To 1000000000
        calc(i, result)
    Next
    Console.WriteLine("A single processor runs 1 billion operations in: ")
    Console.WriteLine(DateTime.Now.TimeOfDay - starttime)



    starttime = DateTime.Now.TimeOfDay

    Parallel.For(0, 1000000000, Function(i) calc(i, result))

    Console.WriteLine("All your processors run 1 billion operations in: ")
    Console.WriteLine(DateTime.Now.TimeOfDay - starttime)
    Console.ReadLine()

End Sub

PS: I did the code for this in VB.net.


1 Answers

If a person can walk 2 miles in 30 minutes, how long will it take 2 people to walk the same 2 miles?

All jokes aside, the documentation at MSDN says:Executes a for (For in Visual Basic) loop in which iterations MAY run in parallel. the keyword here is MAY.

You are letting the CLR do the work and experience says that .net CLR does not always work the way you thought it would.

In my case (copy-pasted the code) single processor - 21.495 seconds, all processors: 7.03 seconds. I have an i7 870 CPU on 32 bit Windows 7.

like image 117
Bhaskar Shrestha Avatar answered Feb 22 '26 01:02

Bhaskar Shrestha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!