Skip to main content
added 16 characters in body
Source Link
Yves Klett
  • 15.7k
  • 5
  • 59
  • 134

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. @Mr.Wizard´s Positive@Max@a and the@acl´s compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072 0.546 0.0716 0.04056 0.01748 0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561 0.359 0.078 1.748 0.01684 0.01048 

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072 0.546 0.0716 0.04056 0.01748 0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561 0.359 0.078 1.748 0.01684 0.01048 

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. @Mr.Wizard´s Positive@Max@a and @acl´s compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072 0.546 0.0716 0.04056 0.01748 0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561 0.359 0.078 1.748 0.01684 0.01048 
deleted 32 characters in body
Source Link
Yves Klett
  • 15.7k
  • 5
  • 59
  • 134

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072    0.546    0.0716    0.04056    0.01748    0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561    0.359    0.078    1.748    0.01684    0.01048 

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072   0.546   0.0716   0.04056   0.01748   0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561   0.359   0.078   1.748   0.01684   0.01048 

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072  0.546  0.0716  0.04056  0.01748  0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561  0.359  0.078  1.748  0.01684  0.01048 
added 1351 characters in body; added 17 characters in body
Source Link
Yves Klett
  • 15.7k
  • 5
  • 59
  • 134

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

A comprehensiveThe timing comparison would be interesting.for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072 0.546 0.0716 0.04056 0.01748 0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561 0.359 0.078 1.748 0.01684 0.01048 

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

A comprehensive timing comparison would be interesting...

To add a bit variety, you could try:

l = RandomChoice[Range[-100, 1], 5000000]; Tr[Clip[l, {0, Infinity}]] > 0 

The timing for different methods of input shows that, unsurprisingly, some solutions are very dependent on the average number of positive elements and others not so much. Positive@Max@a and the compiled f[a] seem to win every time.

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"]; timeAvg = Function[func, Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}], HoldFirst]; a = RandomInteger[{-1*^7, 1}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
4.072 0.546 0.0716 0.04056 0.01748 0.01048 
a = RandomInteger[{-1*^7, 1*^7}, 1*^7]; MemberQ[a, _?Positive] // timeAvg Or @@ Positive[a] // timeAvg Total@UnitStep[-a] =!= Length@a // timeAvg Tr[Clip[a, {0, Infinity}]] > 0 // timeAvg Positive@Max@a // timeAvg f[a] // timeAvg 
0.561 0.359 0.078 1.748 0.01684 0.01048 
Source Link
Yves Klett
  • 15.7k
  • 5
  • 59
  • 134
Loading