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

I believe the difference you are observing is attributable to packed arrays.

The difference between slow and fast is due to the behavior of RandomChoice. Observe:

<< Developer`

RandomChoice[{pr1, 1 - pr1} -> {1, 0}, 1350] // PackedArrayQ
RandomInteger[1, 1350]                       // PackedArrayQ
False

True

You could pack the first output after generation but a better solution is to pack the list {1, 0} in the first argument:

RandomChoice[{pr1, 1 - pr1} -> ToPackedArray@{1, 0}, 1350] // PackedArrayQ
True

In the case of the Export/Import you either need to export the data in the .MX binary format or re-pack it after importing.

Export["fast.m", fast];
Export["fast.mx", fast];

PackedArrayQ /@ Import["fast.m"]
PackedArrayQ /@ Import["fast.mx"]
{False, False}

{True, True}

Repacking:

fixed = ToPackedArray /@ Import["fast.m"];
PackedArrayQ /@ fixed
{True, True}

I believe the difference you are observing is attributable to packed arrays.

The difference between slow and fast is due to the behavior of RandomChoice. Observe:

<< Developer`

RandomChoice[{pr1, 1 - pr1} -> {1, 0}, 1350] // PackedArrayQ
RandomInteger[1, 1350]                       // PackedArrayQ
False

True

You could pack the first output after generation but a better solution is to pack the list {1, 0} in the first argument:

RandomChoice[{pr1, 1 - pr1} -> ToPackedArray@{1, 0}, 1350] // PackedArrayQ
True

In the case of the Export/Import you either need to export the data in the .MX binary format or re-pack it after importing.

Export["fast.m", fast];
Export["fast.mx", fast];

PackedArrayQ /@ Import["fast.m"]
PackedArrayQ /@ Import["fast.mx"]
{False, False}

{True, True}

Repacking:

fixed = ToPackedArray /@ Import["fast.m"];
PackedArrayQ /@ fixed
{True, True}

I believe the difference you are observing is attributable to packed arrays.

The difference between slow and fast is due to the behavior of RandomChoice. Observe:

<< Developer`

RandomChoice[{pr1, 1 - pr1} -> {1, 0}, 1350] // PackedArrayQ
RandomInteger[1, 1350]                       // PackedArrayQ
False

True

You could pack the first output after generation but a better solution is to pack the list {1, 0} in the first argument:

RandomChoice[{pr1, 1 - pr1} -> ToPackedArray@{1, 0}, 1350] // PackedArrayQ
True

In the case of the Export/Import you either need to export the data in the .MX binary format or re-pack it after importing.

Export["fast.m", fast];
Export["fast.mx", fast];

PackedArrayQ /@ Import["fast.m"]
PackedArrayQ /@ Import["fast.mx"]
{False, False}

{True, True}

Repacking:

fixed = ToPackedArray /@ Import["fast.m"];
PackedArrayQ /@ fixed
{True, True}
Source Link
Mr.Wizard
  • 275.5k
  • 35
  • 608
  • 1.5k

I believe the difference you are observing is attributable to packed arrays.

The difference between slow and fast is due to the behavior of RandomChoice. Observe:

<< Developer`

RandomChoice[{pr1, 1 - pr1} -> {1, 0}, 1350] // PackedArrayQ
RandomInteger[1, 1350]                       // PackedArrayQ
False

True

You could pack the first output after generation but a better solution is to pack the list {1, 0} in the first argument:

RandomChoice[{pr1, 1 - pr1} -> ToPackedArray@{1, 0}, 1350] // PackedArrayQ
True

In the case of the Export/Import you either need to export the data in the .MX binary format or re-pack it after importing.

Export["fast.m", fast];
Export["fast.mx", fast];

PackedArrayQ /@ Import["fast.m"]
PackedArrayQ /@ Import["fast.mx"]
{False, False}

{True, True}

Repacking:

fixed = ToPackedArray /@ Import["fast.m"];
PackedArrayQ /@ fixed
{True, True}