Comparing equivalent Scala and C# string-manipulation code - with Scala running much slower I am very early in my journey of learning Scala, and I was doing challenges on hackerrank.com to get used to the syntax. As I finished one (very trivial) challenge, I noticed that the run times seem to be unusually slow.

Thinking that it might be environment-dependent (as the code runs on the HackerRank servers), I decided to quickly reimplement the same code in C#. Interestingly, the code runs 5x faster...

I know that the environment could affect a lot of things, but surely a 5x speed gain for such a trivial operation is strange? They are using Mono for compiling C# code, so both implementations run on Linux. You can get detailed info on their environment here.

The only other idea I have is that, as I am very new to Scala, it is my code which is the problem. So, please take a look at my two implementations below (first in Scala, second in C#) for this problem.

Scala implementation:

object Solution {
    def main(args: Array[String]) {
        println(if (readLine.toLowerCase.filter(x =&gt; x &gt;= 'a' &amp;&amp; x &lt;= 'z').distinct.length == 26) "pangram" else "not pangram")
    }
}


C# implementation:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Main(String[] args) {
        string x = System.Console.ReadLine();
        System.Console.WriteLine((x.ToLowerInvariant().ToCharArray().Where(z =&gt; z &gt;= 'a' &amp;&amp; z &lt;= 'z').Distinct().Count() == 26) ? "pangram" : "not pangram");
    }
}


and let me know how it is possible that the Scala implementation runs that much slower...

I can post screenshots of the run results if needed, but to give an idea - most C# test cases finish in 0.1s and Scala cases take 0.5s.

Can it be the startup time of the runtime? Or do I surrender to the idea that Scala is really that much slower by nature?

The timings were returned by HackerRank's environment (so I realize, of course, that this is nowhere near a benchmark - but nevertheless 5x is crazy).

Scala results:



C# results:
There is no reason for Scala code to be slower than C#.
The different is probably the time that it take to the JVM to load.
To verify that I suggest you to write simple code in C#, Scala &amp; Java which just print "Hello World" and see the different.  My guess is that the Java &amp; Scala will take the same time while the C# will be faster.
Then just do something which do long calculations and see the different again.