Posts

Showing posts from January, 2018

Playing with strings and performance in C#

Image
A few weeks ago, I was solving a problem at HackerRank and after completing a solution that ran  under 100 milliseconds, the program would still timeout during execution. HackerRank limit is usually 1 second, so something was taking more than 900ms. It was the strings! Yes, we know handling strings is costly, but this time there was no way around it.  At the final stage of the problem I had to construct a string over 200k chars long from an array of Boolean values that represented the bits of the number.   Disclaimer : Strings are slow, mainly because they are immutable. Concatenating two strings, creates a third one and possibly 2 for the garbage collector to pickup. Looking through the code I noticed I was simply using " += " instead of StringBuilder.Append method. After using StringBuilder the solution was faster and the submission was approved. It was  fast now, but it got me thinking "how much faster indeed?" which ended with me measuring t...

New programming book published on Amazon

Image
After a relatively long hiatus as a writer I finally completed a new milestone. I coauthored a programming book and published it on Amazon. I must say I am glad that we finally completed it; while it was a fun and rewarding journey, it was a long one and it took a lot of time, effort and coordination.  The book is in Spanish, and it's 500+ pages long. Title: "Empiece a Programar. Un enfoque multiparadigma con C#" Format: Paperback The title translates to "Begin programming. A multi-paradigm approach with C#". The book is written in Spanish, not only because all the authors are Cubans, but also because we believe that the Spanish-speaking community deserves a fresh new book; instead of a translated one. We used C# throughout the book in order to teach programming, and we travel from the simple "Hello World" snippet to arrays, data structures, dynamic programming, recursion, inheritance, SOLID principles, functional programming and conc...