1

In this example I used appendTo.

f[x_] := x + 1;
data = {};
For[x = 1, x <= 10, x++,
f[x];
spec = AppendTo[data, {x, f[x]}]] // Timing // First

Out[37]= 0.000054

ListPlot[spec]

enter image description here

wahlang
  • 11
  • 2

1 Answers1

4

Try

Reap[For[x = 1, x <= 10, x++, f[x];
Sow[{x, f[x]}]
]][[2, 1]] // AbsoluteTiming
(*0.0000701*)

but it seems to be slower than your vewrsion. Use Table for a significant faster version:

Table[{x, f[x]}, {x, 1, 10}]; // AbsoluteTiming
(*{0.0000199, Null}*)
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55