Simple tips to write efficient code: statistical programming!

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:

No alt text provided for this image

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.

To view or add a comment, sign in

More articles by Vincent Béchard, MASc.

  • When was the last time you tried a different search engine?

    I recently got really bored of doing web searches using Google. It used to be great 20 years ago, but now… Google…

    1 Comment
  • Embedding Electric Vehicles in Discrete Events Simulations

    Incorporating electric vehicles (EVs) into operations is a growing trend. Consequently, there is an increasing number…

  • Simple tips to write efficient code

    Programming CPU heavy data processing and analysis tasks? Some of my clients are amazed by the execution speed of my…

  • Optimal Ordering for Smarter Inventory Management

    Get the printer-friendly PDF version I am a simulation expert. To my eyes, a process is an oriented network of steps.

  • What is a model, anyway?

    This is a question I tried to answer many months ago. I was explaining that the traditional boundaries between machine…

  • Nouvelle ère pour notre complément Excel!

    Nouvelles fonctionnalités Oui, nous avons une nouvelle apparence moderne… Mais les changements majeurs sont sous le…

  • New era for our Excel addin!

    New core features Yes, we have a brand-new modern look… But the major changes are under the hood! We added the…

  • Lean Construction to Increase Profitability

    Executing a complex construction project? Experiencing difficulties, dealing with safety, delays and cost issues? Read…

  • Never stop learning: my journey

    After several years of professional life, I became an established expert of something (discrete event simulation). And…

  • Usefulness of 3D simulation for conveyor networks

    Did you know that Différence is your one-stop shop for conveyor network modelling and analytics? Are you scared of your…

Explore content categories