While exploring a recent Linq question i noticed that the algorithm seemed rather slow. Digging deeper i noticed that it wasn't the linq code but the output of the result afterwards that took the long time. (Kudos to Marc Gravel btw for kicking the slickest Linq i've seen yet.)
Code:
DateTime dt = DateTime.Now;
Console.WriteLine("Alg Start " + dt.Second + "." + dt.Millisecond);
var qry = from l in Enumerable.Range(100000, 999999)
let s = l.ToString()
let sReversed = new string(s.Reverse().ToArray())
from i in Enumerable.Range(3, 9)
let t = (l * i).ToString()
where t == sReversed
select new { l, i };
dt = DateTime.Now;
Console.WriteLine("Alg End " + dt.Second + "." + dt.Millisecond);
foreach (var row in qry)
Console.WriteLine("{0} x {1} = {2}", row.l, row.i, row.l * row.i);
dt = DateTime.Now;
Console.WriteLine("Disp End " + dt.Second + "." + dt.Millisecond);
Output:
Alg Start 20.257
Alg End 20.270
109989 x 9 = 989901
219978 x 4 = 879912
1099989 x 9 = 9899901
Disp End 31.322
.13 seconds to calculate and more than 11 to display?!? What's the reason for this?