February 20, 2010 .NET, LambdaExpression, MasterDiploma
February 20, 2010 .NET, LambdaExpression, MasterDiploma
For my Diploma work I need to calculate distance between two vectors.
For example, Euclidean distance is calculated like:
How could it look if I do have two lists with values?
Something like this:
But, please take a look how sweet this all could be with Lambda Expression:
Of course main point of this post is not to show all beauty of LE, but either what did happen after I wrote this Lambda expression. Maniacal interest in what is faster to execute and what is the difference in performance of this two methods appeared in my mind, so I prepared few simple tests.
I’ve ran my tests on 1000000 elements and results were such:
Also, I played with VS performance analyzer (Analyze -> Launch Performance Wizard…).
It allows me run one version of code (Lambda) and get detailed report, then run another version of code (Loop) and get another report. After that I’m able to compare results seeing performance improvements. So that is good tool.
Markdown | Result |
---|---|
*text* | text |
**text** | text |
***text*** | text |
`code` | code |
~~~ more code ~~~~ |
more code |
[Link](https://www.example.com) | Link |
* Listitem |
|
> Quote | Quote |
you could accomplish the same result with Aggregate, you could try it and even test if it is faster than two functions calls as in your example… would be interesting to hear about results!
oops, sorry, I guess you can't because you have two different arrays :(
in Scala there is a great function called zip, that can create an list out of two lists with a pairs: one element from the first list and corresponding element from the second list.
I guess there is no such function in C# ?
Andrii,
at least I do not know about such function, but anyway I think that "zipping" also takes time.
Thanks for so nice comments.
I hope that 01es will not kill me because of my switching to C# instead of Java. I'm 5 times faster with C# and don't have such laziness :)
C# is not actually worse language than Java. It is dynamically developed and improved, it has many great features: closures, functions as arguments, no erasures of generic types and I guess many others, that Java just lacks. So right now C# as language is much better than Java. Strong side of Java is a large ecosystem, but I guess C# is not to far behind.
Oh! And one more feature that I guess every Java programmer miss:
public class POCO
{
public string name { get; set; }
public string id { get; private set; }
}
It takes much more code in Java to do the same :)
You can use all this while persuading Oles that C# is better ;)
And don't forget that C# is not Microsoft's proprietary language! It is standardized language. Good luck!
Yes, java has large ecosystem.
But I think that Microsoft could invest into making C# more wide-spread, but it just doesn't want. And this is crap thing.
Also, I hate writing properties in Java.
Thanks for this nice comment and for hints when I'll have talk with Mr. 01.
I honestly don't think this test is fair to Lamda expression. I could be wrong. I think what Lamda is doing is generating a list and store it into memory first. I think it gets expensive that way and causes the program to slow down.