Skip to content

Instantly share code, notes, and snippets.

@francisbrito
Created May 1, 2013 11:28
Show Gist options
  • Save francisbrito/5494812 to your computer and use it in GitHub Desktop.
Save francisbrito/5494812 to your computer and use it in GitHub Desktop.
A simple performance test written in C# using TPL. (PLINQ)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace PerfTestTpl.ParallelImpl
{
internal class Program
{
private static void Main(string[] args)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var count = 0;
string text;
using (var reader = new StreamReader("./seeds.txt"))
{
text = reader.ReadToEnd();
}
var lines = text.Split('\n');
count += (from line in lines.AsParallel()
select line.Split(' ')
into sb
select new int[]
{
Convert.ToInt32(sb[0]), Convert.ToInt32(sb[1])
}
into b
select PalindromeCountBetween(b[0], b[1])).Sum();
Console.WriteLine("Count: {0}", count);
watch.Stop();
Console.WriteLine("Elapsed: {0} secs", watch.Elapsed.TotalSeconds);
}
public static bool IsPalindrome(int x)
{
var copy = x;
var reverse = 0;
while (copy > 0)
{
reverse = reverse*10 + copy%10;
copy /= 10;
}
return x == reverse;
}
public static int PalindromeCountBetween(int lower, int upper)
{
var result = 0;
for (int i = lower; i <= upper; i++)
{
if (IsPalindrome(i))
{
result++;
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment