TransWikia.com

improve performance of list creation

Mathematica Asked by mrz on April 1, 2021

How can I improve significantly the speed of the following code?

nmax = 10000000;
r = Range[nmax];
res = ({#, #^2, If[Mod[#, 2] == 0, "even", "odd"]} & /@ r); // AbsoluteTiming

{15.3145, Null}

2 Answers

res = Transpose[{r, r^2, Mod[r, 2]}];

This is about 50 times faster than the original code on my machine and contains the same information. I replaced the strings by integers (0 or 1), because that allows one to use vectorization and packed arrays.

If you insist on the strings, you can try

res = Transpose[{r, r^2, {"odd", "even"}[[Mod[r, 2, 1]]]}];

which is still 5 times faster than the original code. But this also shows drastically that transposing an unpackedable array can be very expensive. (Run the code without Transpose and compare the runtime.)

Correct answer by Henrik Schumacher on April 1, 2021

Another trick, not so efficient.

nmax = 1000;
r = Range[nmax];
f[x_] := {x, x^2, If[EvenQ@x, "even", "odd"]};
SetAttributes[f, Listable];
res1 = (f@r); // AbsoluteTiming

{0.0026543, Null}

This is the origin code.

{0.003949, Null}

res1 == res2

True

Answered by wuyudi on April 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP