Skip to main content
improved formulation
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44

This is definitely not memoization. The reason for the observed speed up is that for large arrays (e.g. 10^8 elements), the memory clean up operations may take significantnoticeable time. If one doesn't free memory, one can perform some operations a bit faster.

Here is a simplersimple example:

This evaluation is noticeablya bit faster.

In the example you provide, removing the result of Unitize@data[[All, n]] from memory takes significantsome time. If one saves this array in a redundant variable, one avoids immediate memory clean-up and wins somethe evaluation seems to be faster. In case of pseudo-memoization the Clear[pick, unitize] command will take extra time to free the memory, but this command is placed outside the AbsoluteTiming[] scope. That is why "memoization" seems to speed up the calculation.

This is definitely not memoization. The reason for the observed speed up is that for large arrays (e.g. 10^8 elements), the memory clean up operations may take significant time. If one doesn't free memory, one can perform some operations faster.

Here is a simpler example:

This evaluation is noticeably faster.

In the example you provide, removing the result of Unitize@data[[All, n]] from memory takes significant time. If one saves this array in a redundant variable, one avoids memory clean-up and wins some time.

This is definitely not memoization. The reason for the observed speed up is that for large arrays (e.g. 10^8 elements), the memory clean up operations may take noticeable time. If one doesn't free memory, one can perform some operations a bit faster.

Here is a simple example:

This evaluation is a bit faster.

In the example you provide, removing the result of Unitize@data[[All, n]] from memory takes some time. If one saves this array in a redundant variable, one avoids immediate memory clean-up and the evaluation seems to be faster. In case of pseudo-memoization the Clear[pick, unitize] command will take extra time to free the memory, but this command is placed outside the AbsoluteTiming[] scope. That is why "memoization" seems to speed up the calculation.

improved formulation
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44

This is definitely not memoization. The reason for thisthe observed speed up is that for large arrays (likee.g. 10^8 elements), the memory clean up operations may take significant time. If one doesn't free memory, one can saveperform some timeoperations faster.

Here is ana simpler example:

Let's create a large array, then perform a calculation, and remove the array:

It takes 0.42 seconds. Let's now do the same thing, but keep the array in memory:

This evaluation is noticablynoticeably faster.

Note that, it 0.06 seconds is the difference of the calculation times above.

[Edit: as noted by Carl Woll in comments, This example shows that if one wants to measure symbol-removing-time this way:

garbage = ConstantArray[0, 10^8]; AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise an Out[] variable will retainwe keep the contents of large array garbage. In this case Remove[garbage] will instantly delete the referenceinstead of removing it, but not the large data itselfour code can run faster, because we don't need to spent time on freeing memory. End of edit]

You should put Clear[pick, unitize] inside your timing function. This This test will show that the pseudo-memoization technique is actually slower than built-in functions:

Technical note: as noted by Carl Woll in comments, if one wants to measure the symbol-removing-time using the following code:

In[1] := garbage = ConstantArray[0, 10^8]; In[2] := AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise the Out[1] variable will retain the contents of the large array. If Out[1] retains the large data, Remove[garbage] will only delete the reference, but not the data itself. Deletion time of a reference is almost zero, but it doesn't correspond to the deletion time for large data.

This is definitely not memoization. The reason for this speed up is that for large arrays (like 10^8 elements), the memory clean up operations may take significant time. If one doesn't free memory, one can save some time.

Here is an example:

Let's create a large array, perform a calculation, and remove the array:

Let's now do the same thing, but keep the array in memory:

This evaluation is noticably faster.

Note that, it is the difference of the calculation times above.

[Edit: as noted by Carl Woll in comments, if one wants to measure symbol-removing-time this way:

garbage = ConstantArray[0, 10^8]; AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise an Out[] variable will retain the contents of large array garbage. In this case Remove[garbage] will instantly delete the reference, but not the large data itself. End of edit]

You should put Clear[pick, unitize] inside your timing function. This will show that the pseudo-memoization technique is actually slower than built-in functions:

This is definitely not memoization. The reason for the observed speed up is that for large arrays (e.g. 10^8 elements), the memory clean up operations may take significant time. If one doesn't free memory, one can perform some operations faster.

Here is a simpler example:

Let's create a large array, then perform a calculation, and remove the array:

It takes 0.42 seconds. Let's now do the same thing, but keep the array in memory:

This evaluation is noticeably faster.

Note that 0.06 seconds is the difference of the calculation times above. This example shows that if we keep the large array instead of removing it, our code can run faster, because we don't need to spent time on freeing memory.

You should put Clear[pick, unitize] inside your timing function. This test will show that the pseudo-memoization technique is actually slower than built-in functions:

Technical note: as noted by Carl Woll in comments, if one wants to measure the symbol-removing-time using the following code:

In[1] := garbage = ConstantArray[0, 10^8]; In[2] := AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise the Out[1] variable will retain the contents of the large array. If Out[1] retains the large data, Remove[garbage] will only delete the reference, but not the data itself. Deletion time of a reference is almost zero, but it doesn't correspond to the deletion time for large data.

improved answer
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44

Cause of speed up

This is definitely not a memoization. The reason for thethis speed up is that when you work with veryfor large arrays (like 10^8 elements), the memory clean up operations may take significant time. If you don't cleanone doesn't free memory you, one can save some time.

Here is an example:

Let's create a large array, perform a calculation, and remove the array:

AbsoluteTiming[ Total[ConstantArray[0, 10^8]]; ] 

{0.422509, Null}

Let's now do the same thing, but keep the array in memory:

AbsoluteTiming[ Total[garbage = ConstantArray[0, 10^8]]; ] 

{0.366755, Null}

ItThis evaluation is noticably faster.

Let's check how long does it take to remove athe large array:

AbsoluteTiming[ Remove[garbage] ] 

{0.061982, Null}

ItNote that, it is the difference of the calculation times above.

[Edit: as noted by Carl Woll in comments, if one wants to makemeasure symbol-removing-time this experiment correctlyway:

garbage = ConstantArray[0, 10^8]; AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise an Out[] variable canwill retain the contents of large array garbage. In this case Clear[]Remove[garbage] will instantly delete the reference, but not actually clean the memory and will be executed instantlylarge data itself. ]End of edit]

Your example

In the example you provide, removing the result of Unitize@data[[All, n]] array from memory takes significant time. If one saves this array in a redundant variable, one avoids memory clean-up and wins some time.

How to make testa representative test?

You should put Clear[pick, unitize] inside your timing function. This will show that the pseudo-memoization technique is actually slower than built-in functions:

Table[ Clear[data]; data=RandomInteger[{0,10},{i*10^7,3}]; { Pick[data,Unitize@data[[All,-1]],1]; // AbsoluteTiming // First , Clear[pick,unitize]; unitize[x_]:=unitize[x]=Unitize[x]; pick[xs_,sel_,patt_]:=pick[xs,sel,patt]=Pick[xs,sel,patt]; pick[data,unitize@data[[All,-1]],1]; // AbsoluteTiming // First }, {i,5}] (* {{0.534744, 0.469538}, {1.03776, 1.05842}, {1.58536, 1.65404}, {2.10422, 2.11284}, {2.48129, 2.71405}} *) 

Cause of speed up

This is definitely not a memoization. The reason for the speed up is that when you work with very large arrays (like 10^8 elements), the memory clean up operations may take significant time. If you don't clean memory you save time.

Here is an example:

Let's create a large array, perform a calculation, and remove the array:

AbsoluteTiming[ Total[ConstantArray[0, 10^8]]; ] 

{0.422509, Null}

Let's now do the same thing, but keep array in memory:

AbsoluteTiming[ Total[garbage = ConstantArray[0, 10^8]]; ] 

{0.366755, Null}

It is noticably faster.

Let's check how long does it take to remove a large array:

AbsoluteTiming[ Remove[garbage] ] 

{0.061982, Null}

It is the difference of the calculation times above.

[Edit: as noted by Carl Woll in comments, to make this experiment correctly one should set $HistoryLength to zero, otherwise an Out[] variable can retain the contents of large array. In this case Clear[] will not actually clean the memory and will be executed instantly. ]

Your example

In the example you provide, removing of Unitize@data[[All, n]] array from memory takes significant time. If one saves this array in a redundant variable, one avoids memory clean-up and wins some time.

How to make test representative?

You should put Clear[pick, unitize] inside your timing function. This will show that the pseudo-memoization technique is actually slower:

Table[ Clear[data]; data=RandomInteger[{0,10},{i*10^7,3}]; { Pick[data,Unitize@data[[All,-1]],1]; // AbsoluteTiming // First , Clear[pick,unitize]; unitize[x_]:=unitize[x]=Unitize[x]; pick[xs_,sel_,patt_]:=pick[xs,sel,patt]=Pick[xs,sel,patt]; pick[data,unitize@data[[All,-1]],1]; // AbsoluteTiming // First }, {i,5}] (* {{0.534744, 0.469538}, {1.03776, 1.05842}, {1.58536, 1.65404}, {2.10422, 2.11284}, {2.48129, 2.71405}} *) 

Cause of speed up

This is definitely not memoization. The reason for this speed up is that for large arrays (like 10^8 elements), the memory clean up operations may take significant time. If one doesn't free memory, one can save some time.

Here is an example:

Let's create a large array, perform a calculation, and remove the array:

AbsoluteTiming[ Total[ConstantArray[0, 10^8]]; ] 

{0.422509, Null}

Let's now do the same thing, but keep the array in memory:

AbsoluteTiming[ Total[garbage = ConstantArray[0, 10^8]]; ] 

{0.366755, Null}

This evaluation is noticably faster.

Let's check how long does it take to remove the large array:

AbsoluteTiming[ Remove[garbage] ] 

{0.061982, Null}

Note that, it is the difference of the calculation times above.

[Edit: as noted by Carl Woll in comments, if one wants to measure symbol-removing-time this way:

garbage = ConstantArray[0, 10^8]; AbsoluteTiming[Remove[garbage]] 

one should set $HistoryLength to zero, otherwise an Out[] variable will retain the contents of large array garbage. In this case Remove[garbage] will instantly delete the reference, but not the large data itself. End of edit]

Your example

In the example you provide, removing the result of Unitize@data[[All, n]] from memory takes significant time. If one saves this array in a redundant variable, one avoids memory clean-up and wins some time.

How to make a representative test?

You should put Clear[pick, unitize] inside your timing function. This will show that the pseudo-memoization technique is actually slower than built-in functions:

Table[ Clear[data]; data=RandomInteger[{0,10},{i*10^7,3}]; { Pick[data,Unitize@data[[All,-1]],1]; // AbsoluteTiming // First , Clear[pick,unitize]; unitize[x_]:=unitize[x]=Unitize[x]; pick[xs_,sel_,patt_]:=pick[xs,sel,patt]=Pick[xs,sel,patt]; pick[data,unitize@data[[All,-1]],1]; // AbsoluteTiming // First }, {i,5}] (* {{0.534744, 0.469538}, {1.03776, 1.05842}, {1.58536, 1.65404}, {2.10422, 2.11284}, {2.48129, 2.71405}} *) 
added comment by Carl Woll
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
removed update
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
added update
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
added a benchmark
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
improved answer
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
improved the answer
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading
Source Link
Ray Shadow
  • 8k
  • 1
  • 17
  • 44
Loading