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

The fastest I could come up with, using the undocumented Compile`GetElement for indexing (it's the fastest even without):

f = Compile[ {{l, _Integer, 1}}, Module[ {max = -1}, Do[ If[max < Compile`GetElement[l, i], max = Compile`GetElement[l, i]], {i, 1, Length@l}]; max > 0 ], CompilationTarget -> "C" ] 

Using timeAvg from Mr.Wizard's answeranswer,

MemberQ[a, _?Positive] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg (* 1.38034 0.114101 0.0187749 0.00830531 *) 

The fastest I could come up with, using the undocumented Compile`GetElement for indexing (it's the fastest even without):

f = Compile[ {{l, _Integer, 1}}, Module[ {max = -1}, Do[ If[max < Compile`GetElement[l, i], max = Compile`GetElement[l, i]], {i, 1, Length@l}]; max > 0 ], CompilationTarget -> "C" ] 

Using timeAvg from Mr.Wizard's answer,

MemberQ[a, _?Positive] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg (* 1.38034 0.114101 0.0187749 0.00830531 *) 

The fastest I could come up with, using the undocumented Compile`GetElement for indexing (it's the fastest even without):

f = Compile[ {{l, _Integer, 1}}, Module[ {max = -1}, Do[ If[max < Compile`GetElement[l, i], max = Compile`GetElement[l, i]], {i, 1, Length@l}]; max > 0 ], CompilationTarget -> "C" ] 

Using timeAvg from Mr.Wizard's answer,

MemberQ[a, _?Positive] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg (* 1.38034 0.114101 0.0187749 0.00830531 *) 
Source Link
acl
  • 20k
  • 3
  • 67
  • 99

The fastest I could come up with, using the undocumented Compile`GetElement for indexing (it's the fastest even without):

f = Compile[ {{l, _Integer, 1}}, Module[ {max = -1}, Do[ If[max < Compile`GetElement[l, i], max = Compile`GetElement[l, i]], {i, 1, Length@l}]; max > 0 ], CompilationTarget -> "C" ] 

Using timeAvg from Mr.Wizard's answer,

MemberQ[a, _?Positive] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg (* 1.38034 0.114101 0.0187749 0.00830531 *)