Micro benchmarking: finding the max value

I have seen a Pluralsight course recently on performance by Sasha Goldstein. At one point, he mentioned how he had used processor pipelining to find a maximum number of an array of integers even faster the than the regular for/while loop. This got me thinking, can we get the presented solution even faster? (the catch: he also presented some other features of the clr/framework which will help with this)

So how can get even faster (in certain situations) finding a maximum integer in an unordered array?

In this investigation, I am going to use micro-benchmarking, which is certainly a way of measuring one or a couple of instructions, but I find the results difficult to generalize: things being measured are correct, but small changes to the code or the environment can have significant impact on the results.

In my tests I will use an array of 2MB, which easily fits into the processor cache of my machine. The array is filled with integers [0..10000). It is important to have it the whole array fit in the processor cache, so we can avoid measuring the latency for bringing a chunk of data from memory to the processor.

Find out more


C# Functional style - Part 2

In the previous post, I have looked how I could compose methods together:

int RequestHandler(int x) => LogOutput(Add1(LogInput(x)));

As it is a bit difficult to read (inside out), I would like to see alternative solutions to compose them together while achieving the same results.

For the following methods, it is needed that each method has one input parameter and one result returned. In the following posts, I will use the partial application of arguments and currying to achieve this.

Find out more


Allocation free algorithms

With latest C# we have a handful of new constructs to create more performant applications.

C# Challenge: allocation free algorithms

When talking 'allocation free' we mean heap allocations, while on the stack memory is still going to be allocated similarly as before.

Why is this important?

Find out more


C# Functional style - Part 1

I have been learning about F# and functional programming lately. There is a great F# site explaining the functional concepts fsharpforfunandprofit.

There are some really interesting concepts like Partial Application of Arguments and Currying. I wonder on how we could bring these concepts to C# and how the could would look like with the latestC# 7 syntax.

I will take an example which comes up over-and-over, and walk through this example, improving it each step in C#.

In this part I will take a look at a very simple solution, avoiding using extensions methods and Func types. My goal is not to provide a mathematically equivalent solution, but rather than to see how the new syntax to simplify.

Find out more


Performance of Value Tuples in C# - 2

In the previous post I have shown how value tuples differ from having multiple out parameters. The IL code has been shown and some micro-benchmarking have been done.In this post I would like re-visit one very interesting question around the performance and the generated IL Code. When we know for sure, that one of the parameters of a method is not needed, we can use a (int success, _) syntax to tell the C# compiler that we are only interested in the first parameter of the returned value tuple.

Here is the IL when we have both parameters used:

value-out

The previous post has shown that the average of deconstructing both parameters takes 18959 ms in that given test environment.

Find out more