Thursday, February 4, 2016

Adding/removing keyboard layouts with PowerShell

Sometimes I need to type in German or in Russian. Often enough – every week I type something. Meanwhile, it bugs me to find the right layout every time.

And since I now prefer PowerShell to cmd.exe, I had a thought – I should add these layouts as I need them. So, now I have two files:

addDe.ps1:
$1 = Get-WinUserLanguageList
$1.Add('de-DE')
Set-WinUserLanguageList $1 -Force

remDe.ps1:
$1 = Get-WinUserLanguageList
$1.RemoveAll( { $args[0].LanguageTag -clike 'de*' } )
Set-WinUserLanguageList $1 -Force

They're simple and awesome.

Friday, December 18, 2015

The price of async signatures in .Net/C#

Some code is async in nature (e.g. I/O-bound) and you should write it async.
On the other side of the fence is CPU-bound code and there's no point in writing it async.

One day, as you design a library, you need to decide of whether you should make method's signatures async for the code that may as well as may not be async in nature.

Async comes at a price: CPU cycles & memory footprint.

So, I wrote a benchmark: https://gist.github.com/modosansreves/51391d38b59f68f1ae01

It runs a simple cycle adding numbers, just to keep CPU busy for a while. The best thing now is to read the code, it speaks for itself.

Here are results for my i5 machine.

Empty cycles: 10
Repetitions: 10000
Best result for pure  sync code: 234
Best result for sync async code: 464
Empty cycles: 100
Repetitions: 10000
Best result for pure  sync code: 1918
Best result for sync async code: 2265
Empty cycles: 1000
Repetitions: 10000
Best result for pure  sync code: 17685
Best result for sync async code: 17869

This benchmark ignores GC pressure, thou.

Verdict: it's totally ok to design interfaces around Task.