Fastest way to write a very long expression to a file.

Hello everyone,

I was wondering what is the fastest way to write out a very long expression to a file in a human readable format, lets say InputForm with ASCII encoding.
A suitable benchmark could be

expression = Plus @@ Table[1/n x^n, {n, 1, 2000000}];

or something with more terms.
Using AbsoluteTiming, I see similar performance between Write and WriteString. Moreover, it looks like most of the time is spend converting the expression into a string

AbsoluteTiming[exprStr = ToString[expression, InputForm];]

Is this indeed the performance bottleneck, and if so, is there a way how the step can be speeded up?

Hi Tom,

Can you elaborate on why it has to be human readable? Is someone actually going to read an expression with 2 million terms?

Hi Rohit,

that might indeed take a bit longer than a few seconds!

What I actually meant by this is that the final output should not be e.g. binary data, but the uncompressed expression. I am trying to generate a large amount of source code.