# Timing: how make a program to work faster using built in functions and tables

I want to improve my code to make it working faster even for very large numbers.

fun[n_, a_, b_] := (
low = n^a;
low2 = n^b;
p = Table[Binomial[n, x]*StirlingS2[a, x]*x!/low, {x, 1, a}];
pp = Table[Binomial[n, x]*StirlingS2[b, x]*x!/low2, {x, 1, b}];
ListPlot[{p // N, pp // N}, some options]
)


I know that I should use as many built-in functions as I can, so I made it. And also I should not repeat the same thing many times. I want to run this function for values like for example $n = 1000000, a = 800000, b = 800000$. Is it possible to improve the speed of the program? And is it possible to evaluate the function for such huge numbers in a reasonable time (for example less than 2 hours)? Maybe is any way to make working on tables faster?

• Try p=Binomial[n,#] StirlingS2[a,#] #!/low/@Range[a], functional programming is often faster than procedural programming in Mathematica. (Map, that is /@, is functional and Table is procedural.) – C. E. Jul 11 '14 at 8:54
• @Pickett Are you sure it works good, because when I used that it has problem to understand the sign # and no plot is generated, because the values are not evaluated correctly – Ziva Jul 11 '14 at 9:01
• Sorry, there is a typo. It should be &/@Range[a]. Look up "pure functions" in the documentation. – C. E. Jul 11 '14 at 9:06
• @Pickett Yes, I found this typo. Thanks, this modification really increased a little bit the speed, but still not enough for huge numbers. – Ziva Jul 11 '14 at 9:20
• Do you have some small n,a,b and the picture,so that I can test my code? – Apple Jul 11 '14 at 9:33