MasterDiploma

Lambda Expression or Simple Loop

February 20, 2010 .NET, LambdaExpression, MasterDiploma 6 comments

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:

         public double GetDistance(IList<double> firstVector, IList<double> secondVector)
         {
             double sum = 0;
             for (int i = 0; i < firstVector.Count; i++)
             {
                 sum += (firstVector[i] – secondVector[i]) * (firstVector[i] – secondVector[i]);
             }
             return Math.Sqrt(sum);

But, please take a look how sweet this all could be with Lambda Expression:

         public double GetDistance(IList<double> firstVector, IList<double> secondVector)
         {
             double sum = firstVector.Select((x, i) => (x – secondVector[i]) * (x – secondVector[i])).Sum();
             return Math.Sqrt(sum);
         }

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:

  1. Lambda expressions are quite slower than simple loops.
  2. My test showed that primitive loop is faster in about 15-20%.

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.


6 comments


Master Diploma: Self-Organizing Maps Parallelization

January 30, 2010 MasterDiploma 1 comment

It’s time to start seriously work on my Master Diploma.

What about is my Master Diploma work?

In Artificial Intelligence we often use neural networks in try to simulate aspects of the biological neural networks. Applicability of these is very wide, it could be smart unit in some video game, or expert subsystem when diagnosis patient. Even your inbox e-mails are filtered with algorithm, which includes neural networks usage.

One great applicability of Self-Organizing Maps (one of the artificial neural networks) is possibility to analyze high-dimensional data, save its topology and translate it to low-dimensional representation which further could be easily analyzed by human brains.

For human being it is hard to work with dozens of input information for different aspects of one thing. For example 100 patients being diagnosing and for each we need 30-40 different measurements. SOM could easily classify data and for output with knowing of some existing diagnosis we can get “Patient – Diagnosis”.

I mentioned something like “with knowing of some existing…”. This means that SOM requires training, which takes execution time. If our application domain is something for military, time could be critical constraint.

In this work I’m going to significantly improve calculations of Self-Organizing Maps with introducing multi-threading improvements.

Project hosting

I already hosted my project with google code:
http://code.google.com/p/andriybudaydiploma/


Code:
The
self-organizing map is a popular unsupervised neural network model for
high-dimensional data analysis. However, the high execution times
required to train the map put a limit to its use in many application
domains, where either very large datasets are encountered and/or
real-time response times are required.

Project is host for implementation of a parallel algorithm, purpose of
which is to significantly improve calculations of standard algorithm.
This is main goal of actual project.

This project represents Andriy Buday’s Master Diploma work.

e-mail me: andriybuday@gmail.com

What do I need to accomplish?

I need to ensure that I understand computations of SOM deeply.
Research over interned for similar works and take them into account.
I’ve decided to write my code from scratch, since I already have some implementation of SOM.
After I’ll have some implementation I need to analyze results I got on different systems.
Improve my knowledge of Java, Concurrency and train my scientific habits. :)

This blog and my Diploma

I was thinking about posting status of my work on Diploma on separate blog or site, but don’t want to have myseft scattered over different locations, so I’ve decided that just tag “Master Diploma” should be enough. I don’t think that it could make sense to hide something from the world or my co-workers or Customers (don’t even think they read my blog). And of course my Diploma is part of my Road To Success.

Please, wish me luck on this!

No doubt I will do the best to take my work to the breakthrough.


1 comment