3
$\begingroup$

I have a list containing 100 complex numbers lying between 1.9 + 1.9 i and 2.0 + 2.0 i you can download its mx file from the link below and import it in Mathematica:

http://adambarfib.ir1.rapidpars.com/17830/18651983/s4fkhv5c31j/vec.mx

Now if you perform a simple calculation with this imported list:

vec = Import["vec.mx"];
c = Table[1.0 + 2.0 I + n (0.01 - 0.01 I), {n, 1, 100}];
AbsoluteTiming[Do[vec.c, 1000]]

It takes 0.0070265 seconds on my laptop. but a similar calculation with a randomly created list, takes half time:

randomVec = RandomComplex[{1.9 + 1.9 I, 2.0 + 2.0 I}, 100];
AbsoluteTiming[Do[randomVec.c, 1000]]

This one takes 0.0036907 seconds.

Could anyone help me what is the problem with my vec?

$\endgroup$
  • $\begingroup$ Very strange, I observe the contrary. MMA12.1 on Windows 10. I increased the loop to 100'000 and get nearly 10 times faster evaluation time with your data:AbsoluteTiming[Do[vec.c, 100000]] -> {0.017462, Null}, AbsoluteTiming[Do[randomVec.c, 100000]] -> {0.140501, Null} If I may speculate, perhaps you data has some structure that can be exploited? $\endgroup$ – Daniel Huber Oct 26 at 10:02
  • 1
    $\begingroup$ Please keep in mind that .mx is not a good format for sharing data. There's no guarantee that someone else can import it, since it's a system-dependent format. $\endgroup$ – Sjoerd Smit Oct 26 at 11:25
5
$\begingroup$

vec array is not packed, search questions related to PackedArray

vec = Import["vec.mx"];
Developer`PackedArrayQ[vec]
c = Table[1.0 + 2.0 I + n (0.01 - 0.01 I), {n, 1, 100}];
RepeatedTiming[Do[vec.c, 1000]]

vec = Developer`ToPackedArray[vec] ;
Developer`PackedArrayQ[vec]
c = Table[1.0 + 2.0 I + n (0.01 - 0.01 I), {n, 1, 100}];
RepeatedTiming[Do[vec.c, 1000]]
(* False *)
(* {0.0038056587301587307`3.,Null} *)
(* True *)
(* \
{0.002334895238095238`2.,Null} *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.