Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello everyone,
I'm writing a class for calculations with complex numbers because the built in Complex-liberary of VB.NET had flaws i couldn't live with. During performance testing I noticed that my liberary is slower (which is ok, i'm not a genius ;-)). What struck me was that my classes constructor was a lot slower.

On one million cycles it was 10 times slower than the one of the built in complex-class.

My classes constructor looks like this:
VB
Class test
    Private re As Double
    Private im As Double

    Public Sub New(Real As Double, Imaginary As Double)
       Me.re = Real
       Me.im = Imaginary
       refreshar = True
       refreshmag = True
    End Sub
    [...]
End Class


So there really is not that much going on and still it takes way longer to execute than the one of the built in class.

Does anyone have some pointer on what might be the cause of this?

Thank you in advance, and if you need further infos please ask.

Best regards
Posted
Comments
Zoltán Zörgő 4-May-13 17:43pm    
First of all, I suggest make your tests with a release mode build, and from outside VisualStudio.
Keex0r 4-May-13 17:51pm    
Ok I did this, which improved the speed. But it also improved the speed of the built-in class so mine is still way slower.
Sergey Alexandrovich Kryukov 5-May-13 0:57am    
Pretty good question, I voted 4.
—SA

1 solution

I'm not an expert but I noticed that built in Complex is not a Class but a Structure. Did you try using a Structure yourself (object allocation should be faster, but I don't know how you performed the test)?
In any case you may possibly have look at generated MSIL for the two different constructors.
 
Share this answer
 
Comments
Keex0r 4-May-13 17:56pm    
Oh now I feel a little dumb. You are right, it was the difference between Structure<->Class I did not notice. That improved it to nearly completely matching the speed.

Thank you.

Also thanks Zoltan, the reminder of the Built-type was an important one too, and overlooked by me.
CPallini 4-May-13 18:09pm    
You are welcome.
Sergey Alexandrovich Kryukov 5-May-13 0:59am    
I don't think you a dump at all, so I up-voted this question; even though for 99% questions on this forum, these days, even the vote of 1 would be too much. I guess the level of dumbness depends on comparison. :-)
—SA
Sergey Alexandrovich Kryukov 5-May-13 0:56am    
Of course, such "complex" (in another sense of the word, non-primitive) types with numeric semantic should be structs, but I cannot really see why it can improve performance dramatically. I guess, it depends on operations typically performed on these types. So, I can only trust OP's observation. My 5.
—SA
CPallini 5-May-13 12:47pm    
Allocation performance should be better because they are allocated on the stack, I guess.
By the way: Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900