Skip to main content
formatting
Source Link

Notice _?Positive _?Positive is the fastest and most concise.

My intent above was only to compare ?Positive?Positive with x_/;Positive[x] x_ /; Positive[x]. Afterwards I realized a faster way to count the positive numbers in a long list is the follwing.

In:= data=RandomReal[{-0following.2,1},10^7];
Timing[Count[Positive[data],True];]

Out= {0.998,Null}

In:= data=RandomReal[{-0.2,1},10^7]; Timing[Count[Positive[data],True];] Out= {0.998,Null} 

Next we see the timing differences of ConditionCondition and PatternTestPatternTest are different for a more complicated example.

In:= data = RandomInteger[{-100,100},10^7];
Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];]

Out= {20.109,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];]

Out= {10.717,Null}

In:= data = RandomInteger[{-100,100},10^7];
test[n_] = And[Positive[n],EvenQ[n]];
Timing[ Count[data, x_?test[x]]; ]

Out= {0.546,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ]

Out= {0.515,Null}

In:= data = RandomInteger[{-100,100},10^7]; Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];] Out= {20.109,Null} In:= data = RandomInteger[{-100,100},10^7]; Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];] Out= {10.717,Null} In:= data = RandomInteger[{-100,100},10^7]; test[n_] = And[Positive[n],EvenQ[n]]; Timing[ Count[data, x_?test[x]]; ] Out= {0.546,Null} In:= data = RandomInteger[{-100,100},10^7]; Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ] Out= {0.515,Null} 

In the next example the definition is used because the integer has only factors larger than 10^15$10^{15}$. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

Notice _?Positive is the fastest and most concise.

My intent above was only to compare ?Positive with x_/;Positive[x]. Afterwards I realized a faster way to count the positive numbers in a long list is the follwing.

In:= data=RandomReal[{-0.2,1},10^7];
Timing[Count[Positive[data],True];]

Out= {0.998,Null}

Next we see the timing differences of Condition and PatternTest are different for a more complicated example.

In:= data = RandomInteger[{-100,100},10^7];
Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];]

Out= {20.109,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];]

Out= {10.717,Null}

In:= data = RandomInteger[{-100,100},10^7];
test[n_] = And[Positive[n],EvenQ[n]];
Timing[ Count[data, x_?test[x]]; ]

Out= {0.546,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ]

Out= {0.515,Null}

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

Notice _?Positive is the fastest and most concise.

My intent above was only to compare ?Positive with x_ /; Positive[x]. Afterwards I realized a faster way to count the positive numbers in a long list is the following.

In:= data=RandomReal[{-0.2,1},10^7]; Timing[Count[Positive[data],True];] Out= {0.998,Null} 

Next we see the timing differences of Condition and PatternTest are different for a more complicated example.

In:= data = RandomInteger[{-100,100},10^7]; Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];] Out= {20.109,Null} In:= data = RandomInteger[{-100,100},10^7]; Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];] Out= {10.717,Null} In:= data = RandomInteger[{-100,100},10^7]; test[n_] = And[Positive[n],EvenQ[n]]; Timing[ Count[data, x_?test[x]]; ] Out= {0.546,Null} In:= data = RandomInteger[{-100,100},10^7]; Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ] Out= {0.515,Null} 

In the next example the definition is used because the integer has only factors larger than $10^{15}$. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

Mentioned that one of my examples could be 5x faster by avoiding use of PatternTest or Condition. I also show timing results for different ways of coding And[Positive, EvenQ].
Source Link
Ted Ersek
  • 7.2k
  • 20
  • 42

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];] 

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];] 

Out= {5.164,Null}

Notice _?Positive is the fastest and most concise.

My intent above was only to compare ?Positive with x_/;Positive[x]. Afterwards I realized a faster way to count the positive numbers in a long list is the follwing.

In:= data=RandomReal[{-0.2,1},10^7];
Timing[Count[Positive[data],True];]

Out= {0.998,Null}

Next we see the timing differences of Condition and PatternTest are different for a more complicated example.

In:= data = RandomInteger[{-100,100},10^7];
Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];]

Out= {20.109,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];]

Out= {10.717,Null}

In:= data = RandomInteger[{-100,100},10^7];
test[n_] = And[Positive[n],EvenQ[n]];
Timing[ Count[data, x_?test[x]]; ]

Out= {0.546,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ]

Out= {0.515,Null}


Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /; (Condition) is very helpful here, and PatternTest can't be used in its place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ] 

The definition of foo isn't used in the first example because the integer has small factors.

In:= foo[10^40+543] 

Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] 

Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use Module instead of With. However, it doesn't work if I use Block.

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];] 

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];] 

Out= {5.164,Null}

Notice _?Positive is the fastest and most concise.

Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /; (Condition) is very helpful here, and PatternTest can't be used in its place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ] 

The definition of foo isn't used in the first example because the integer has small factors.

In:= foo[10^40+543] 

Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] 

Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use Module instead of With. However, it doesn't work if I use Block.

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];] 

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];] 

Out= {5.164,Null}

Notice _?Positive is the fastest and most concise.

My intent above was only to compare ?Positive with x_/;Positive[x]. Afterwards I realized a faster way to count the positive numbers in a long list is the follwing.

In:= data=RandomReal[{-0.2,1},10^7];
Timing[Count[Positive[data],True];]

Out= {0.998,Null}

Next we see the timing differences of Condition and PatternTest are different for a more complicated example.

In:= data = RandomInteger[{-100,100},10^7];
Timing[Count[data,x_?(Function[n,And[Positive[n],EvenQ[n]]])];]

Out= {20.109,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_/;And[Positive[x],EvenQ[x]] ];]

Out= {10.717,Null}

In:= data = RandomInteger[{-100,100},10^7];
test[n_] = And[Positive[n],EvenQ[n]];
Timing[ Count[data, x_?test[x]]; ]

Out= {0.546,Null}

In:= data = RandomInteger[{-100,100},10^7];
Timing[ Count[data, x_?(And[Positive[#],EvenQ[#]])&]; ]

Out= {0.515,Null}


Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /; (Condition) is very helpful here, and PatternTest can't be used in its place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ] 

The definition of foo isn't used in the first example because the integer has small factors.

In:= foo[10^40+543] 

Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] 

Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use Module instead of With. However, it doesn't work if I use Block.

formatting
Source Link
Verbeia
  • 34.5k
  • 10
  • 111
  • 234

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];]

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];] 

Out= {5.897,Null}

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];]

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];] 

Out= {5.164,Null}

Out= {5.164,Null}Notice _?Positive is the fastest and most concise.

Notice _?Positive is the fastest and most concise.

Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /;/; (ConditionCondition) is very helpful here, and PatternTestPatternTest can't be used in it'sits place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ]

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ] 

The definition of foofoo isn't useedused in the first example because the integer has small factors.

In:= foo[10^40+543] Out= foo[10000000000000000000000000000000000000543]

In:= foo[10^40+543] 

Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of ConditionCondition we would have to evaluate FactorIntegerFactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] Out= {{1000000000000001093,1},{100000000000000000001303,1}}

In:= foo[100000000000000109301303000000000001424179] 

Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use ModuleModule instead of WithWith. However, it doesn't work if I use BlockBlock.

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];]

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];]

Out= {5.164,Null}

Notice _?Positive is the fastest and most concise.

Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /; (Condition) is very helpful here, and PatternTest can't be used in it's place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ]

The definition of foo isn't useed in the first example because the integer has small factors.

In:= foo[10^40+543] Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use Module instead of With. However, it doesn't work if I use Block.

I tried several variations and here are the two fastest ways I can find to do a simple task.

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, x_/;Positive[x] ];] 

Out= {5.897,Null}

In:= data = RandomReal[{-0.2,1}, 10^7]; Timing[ Count[ data, _?Positive ];] 

Out= {5.164,Null}

Notice _?Positive is the fastest and most concise.

Next I illustrate a point Mr. Wizard mentioned without an example above. Use of /; (Condition) is very helpful here, and PatternTest can't be used in its place.

In:= OnlyLargeFactors[factored_]:= 10^15 < Min[Part[factored,All,1]]; foo[n_Integer]:= With[{factors=FactorInteger[n]}, factors/;OnlyLargeFactors[factors] ] 

The definition of foo isn't used in the first example because the integer has small factors.

In:= foo[10^40+543] 

Out= foo[10000000000000000000000000000000000000543]

In the next example the definition is used because the integer has only factors larger than 10^15. If not for the use of Condition we would have to evaluate FactorInteger twice and that can take a long time.

In:= foo[100000000000000109301303000000000001424179] 

Out= {{1000000000000001093,1},{100000000000000000001303,1}}

I am using Mathematica version 7, and I the last example works if I use Module instead of With. However, it doesn't work if I use Block.

Source Link
Ted Ersek
  • 7.2k
  • 20
  • 42
Loading