Skip to content

Instantly share code, notes, and snippets.

@brandonc
Created June 22, 2011 20:46
Show Gist options
  • Save brandonc/1041133 to your computer and use it in GitHub Desktop.
Save brandonc/1041133 to your computer and use it in GitHub Desktop.
Reduce an array using regex in C# and ruby
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Program
{
class XFiles
{
static int Main(string[] argv)
{
string[] names = { "Mark", "Richard", "Fox", "Scully" };
var r = new Regex("(Fox|Scully)");
var xfiles = names.Where(name => r.IsMatch(name)).ToArray();
Console.WriteLine("Everyone: {0}", String.Join(", ", names));
Console.WriteLine(" X-Files: {0}", String.Join(", ", xfiles));
return 0;
}
}
}
names = ["Mark", "Richard", "Fox", "Scully"]
xfiles = names.select do |name|
name =~ /(Fox|Scully)/
end
puts "Everyone: #{names.join(", ")}"
puts " X-Files: #{xfiles.join(", ")}"
@brandonc
Copy link
Author

Stopwatch 100 executions of each using simple shell script (windows / mingw32 bash)

Ruby

$ time xfiles100-rb.sh > /dev/null

real    0m3.955s
user    0m0.438s
sys     0m0.792s

C#

$ time xfiles100-cs.sh > /dev/null

real    0m5.029s
user    0m0.409s
sys     0m0.531s

@brandonc
Copy link
Author

193 vs 569 chars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment