Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Map is automatically compiled. Yes, even with RandomChoice. Try it:

f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, 
  Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t]
 ];
f // InputForm (* -> clean bytecode *)

Check its performance:

p = 0.1;
t = Table[1, {10^6}];
SeedRandom[1000];

AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &,t];] (* -> 0.578125 seconds *)

AbsoluteTiming[c = f[0.1, t];] (* -> 0.593750 seconds *)

The timings are the same, so this is the solution. It's also the reason that Map doesn't unpack and MapThread does. (Compiled code only works with packed arrays.)

MapThread doesn't have this ability and uses the uncompiled code. Map will be the same if it is not allowed to compile, as PickettPickett shows in his answerhis answer.

Map is automatically compiled. Yes, even with RandomChoice. Try it:

f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, 
  Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t]
 ];
f // InputForm (* -> clean bytecode *)

Check its performance:

p = 0.1;
t = Table[1, {10^6}];
SeedRandom[1000];

AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &,t];] (* -> 0.578125 seconds *)

AbsoluteTiming[c = f[0.1, t];] (* -> 0.593750 seconds *)

The timings are the same, so this is the solution. It's also the reason that Map doesn't unpack and MapThread does. (Compiled code only works with packed arrays.)

MapThread doesn't have this ability and uses the uncompiled code. Map will be the same if it is not allowed to compile, as Pickett shows in his answer.

Map is automatically compiled. Yes, even with RandomChoice. Try it:

f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, 
  Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t]
 ];
f // InputForm (* -> clean bytecode *)

Check its performance:

p = 0.1;
t = Table[1, {10^6}];
SeedRandom[1000];

AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &,t];] (* -> 0.578125 seconds *)

AbsoluteTiming[c = f[0.1, t];] (* -> 0.593750 seconds *)

The timings are the same, so this is the solution. It's also the reason that Map doesn't unpack and MapThread does. (Compiled code only works with packed arrays.)

MapThread doesn't have this ability and uses the uncompiled code. Map will be the same if it is not allowed to compile, as Pickett shows in his answer.

Source Link
Oleksandr R.
  • 23.1k
  • 4
  • 87
  • 126

Map is automatically compiled. Yes, even with RandomChoice. Try it:

f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, 
  Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t]
 ];
f // InputForm (* -> clean bytecode *)

Check its performance:

p = 0.1;
t = Table[1, {10^6}];
SeedRandom[1000];

AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &,t];] (* -> 0.578125 seconds *)

AbsoluteTiming[c = f[0.1, t];] (* -> 0.593750 seconds *)

The timings are the same, so this is the solution. It's also the reason that Map doesn't unpack and MapThread does. (Compiled code only works with packed arrays.)

MapThread doesn't have this ability and uses the uncompiled code. Map will be the same if it is not allowed to compile, as Pickett shows in his answer.