Simple tips to write efficient code: statistical programming!
I previously published this article on good programming practices in general : https://bit.ly/35hBzHc. This time I am sharing some interesting results on code performance. I needed to implement an algorithm in R that I had previously written in pure C#. I ended with 2 versions:
- Version 1 : direct translation of C# code to R, with explicit loops and using only custom functions
- Version 2 : implementation that leveraged the native features such as implicit loops and vectorized operations
Short illustration: if you need to shuffle a vector, an efficient implementation in C# is:
int[] randomNumbers = Shuffle(Enumerable.Range(0, 11), new Random()).ToArray();
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random)
{
T[] list = source.ToArray();
int count = list.Length;
while (count > 1)
{
int index = random.Next(count--);
T temp = list[index];
list[index] = list[count];
list[count] = temp;
}
return list;
}
And in R, it is:
v <- sample(v)
In a statistical programming environment, data manipulations are quite easier! Coming back to my to-be-translated-algorithm, not only the Version 2 code was much shorter, but it was faster on various problem sizes:
Bottom line : leveraging the native built-in features of a programming environment can save time to edit the code and time to execute the code!
By Vincent Béchard, Associate and Analytical Decision Specialist at Différence GCS Inc., on November 10, 2020.