From: Brad Wilson Date: 2005-06-26T15:06:25+09:00 Subject: Re: I saw the beauty of Ruby Re: 1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds For comparison, the port of your code to (less than elegant) C#. Timings done with a release build against .NET 1.1 on Windows XP (P4 2.8/800/HT). New to the list, so hopefully I'm not dup'ing someone else. (I adjusted the Ruby version slightly to move the puts call outside the timing loop for code parity, which not surprisingly cut off 0.03s from the Ruby running time.) C:\>ruby primes.rb 5133 time taken 0.44 seconds C:\>primes 5133 Time taken: 00:00:00.0156250 ~ ~ ~ using System; using System.Collections; public class Primes { public static ArrayList CalcPrimes(int val) { ArrayList results = new ArrayList(); if (val < 2) return results; results.Add(2); for (int i = 3; i < val; i += 2) { int r = (int) Math.Sqrt(i); int f = 0; for (int idx = 0; idx < results.Count; idx++) { f = (int) results[idx]; if ((i % f) == 0 || f > r) break; } if ((i % f) != 0) results.Add(i); } return results; } public static void Main() { DateTime start = DateTime.Now; ArrayList primes = CalcPrimes(50000); TimeSpan taken = DateTime.Now - start; Console.WriteLine(primes.Count); Console.WriteLine("Time taken: {0}", taken); } }