Powered by Squarespace
This form does not yet contain any fields.

    Entries in .NET (3)

    Thursday
    Jul162009

    Microbenchmarking

    Towards my previous performance posts:

    Shawn Hargreaves wrote a post describing the way good microbenchmarking of language or platform features should be performed. As I can see, he likes idea of using LINQ for mean and variance calculations too :)

    Wednesday
    Jul082009

    LINQ

    I really like LINQ feature in .NET. For me it’s what people call syntactic sugar. It allows me to reduce amount of code I’m writing dramatically in many cases. Look at the following example - http://pastebin.com/d10c04029

    This code estimates mean and variance of distribution given a sequence of samples. Two versions are provided. The first one uses .NET 2.0 features only and the second uses LINQ. As you can see, LINQ version is very short and awesome-looking even for this very simple example.

    Are there any drawbacks? Yeah, unfortunately. And, of course, that drawbacks are performance related. Let’s compare time required for processing sequence (System.Array of double) containing 50 million samples (drawn from [0,1]-uniform distribution if that matter something for someone):

    Normal: mean=0,5000 var=0,0833 time=00:00:03.6591708
    LINQ: mean=0,5000 var=0,0833 time=00:00:04.5832141

    Why is the LINQ version 1.5 times slower? Well, the obvious answer is that it should call a function (which is not inlined) for every member of the sequence, at least while calculating variance, and the first version should not. And, probably, computational cost of a function call is comparable to a few math operations (as it should be). So, use LINQ as a syntactic sugar only when the function you want to apply to the sequence elements is much more complex than a few additions and multiplications or when your collection is small and you just don’t care about processing time.

    Read more about LINQ performance there - http://davepeck.org/linq-collection-performance/.

    Strange observation: when passing List<double> instead of array, LINQ version takes about 6 seconds while performance of the normal version does not change.

    Wednesday
    Feb112009

    C++ vs C#

    I totally hate C++. And every day my hate grows up. But that is not just about my programming language religion. We should look deeper.

    I know C++ pretty well. I’ve read all the stuff like Effective C++, More Effective C++ and Modern C++ Design and understand all of it. And it was really hard. Now I can do amazing things with templates and write robust error-free code. Do I want it? No. It is still really hard for me (and for everyone, I guess) to write, read and maintain such a code. That snippet looks really ugly in C++:
    for (std::vector<double>::iterator it = values.begin();
    it != values.end();
    ++it)
    DoStuff(*it);
    And here is it’s C# equivalent:
    foreach (double value in values)
    DoStuff(value);
    And I’m pretty sure (I have some experience, you know) that 99% of equivalent code pieces are much more simple in C# than in C++. When you read and maintain a lot of code, it is very important.
    Next, I hate includes. There is no good reason for good programming language to have such a non-obvious and non-robust way to incorporate different subsystems or just pieces of code. Really! IDE can do it for me! In .NET there are no problems with include paths. And I can just reference some library to get access to it’s components. No precompiled headers, no include (or lib) directory setup. That’s all about rapid and easy development.
    And the last thing that’s important for me: in most cases (except of writting really performance-sensitive code) you should not care about memory deallocation at all. Execution environment can do it for you. Rather quickly. And it does. How much errors have you ever had with memory allocation and deallocation in C++? I have a lot. More than any other kinds of errors. It’s like a pain.

    But, of course, C++ is fast. Really fast. In computer vision and 3D rendering there are a lot of very performance-sensitive stuff. And the only choise you have is to code it in C++. But don’t forget: there are wrappers for everything. There is XNA, Emgu.CV and CUDA.NET. There is much more than that. You can still write performance-sensitive code in a managed environment with all it’s benefits.

    I forget to mention wonderful tools for .NET developer such a Resharper, FxCop, StyleCop and many others. There are no technical possibilities to create such a useful and powerful tool for a non-managed language like C++.

    Let’s sum up:
    .NET (C#)
    1. Easy language to code, read and maintain.
    2. No problems with library version control, includes and stuff.
    3. Fast and robust garbage collection.
    4. Very powerful standart library.
    5. Cool IDE and tools.
    6. Quite fast.
    C++
    1. Really fast.
    2. There is Boost.
    So that’s my proposal: when you want to code something, think about implementing it in C# first and consider C++ as the last option. I’m pretty sure you’ll like it as much as I do.