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

Mr.Wizard posed this questionthis question when discussing another problem.

Mr.Wizard posed this question when discussing another problem.

Mr.Wizard posed this question when discussing another problem.

Tweeted twitter.com/StackMma/status/760327795465654272
deleted 27 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

But I think these cases usually won't occur, so I still want to know: are there any methods avaliableavailable to tell Mathematica, "I know testing result of elements will be the same in each trial, SKIP the repeated testing!"?

###Some Notes

I do NOT need a way to store the result of the pattern test, I'm trying to reduce the testing repetitions.

The latter one will speed up the simple pattern matching process in long lists considerably; the first one won't, as the pattern test completes in a flash, but there're are simply too many flashes, thus the overall speed is low.

Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can avoid the testing of the latter three 1, the whole process will be 4 times faster!

  • I do NOT need a way to store the result of the pattern test, I'm trying to reduce the testing repetitions.

  • The latter one will speed up the simple pattern matching process in long lists considerably; the first one won't, as the pattern test completes in a flash, but there're are simply too many flashes, thus the overall speed is low.

  • Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can avoid the testing of the latter three 1, the whole process will be 4 times faster!

This is not simply a matter handing the somewhat unusual case of a stateful test function (e.g. iflike the test function wereone with (EvenQ[# + i++] &)flag) as it is baked into immutableexpressly stateless patterns as well:

But I think these cases usually won't occur, so I still want to know: are there any methods avaliable to tell Mathematica, "I know testing result of elements will be the same in each trial, SKIP the repeated testing!"?

###Some Notes

I do NOT need a way to store the result of the pattern test, I'm trying to reduce the testing repetitions.

The latter one will speed up the simple pattern matching process in long lists considerably; the first one won't, as the pattern test completes in a flash, but there're are simply too many flashes, thus the overall speed is low.

Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can avoid the testing of the latter three 1, the whole process will be 4 times faster!

This is not simply a matter handing the somewhat unusual case of a stateful test function (e.g. if the test function were (EvenQ[# + i++] &)) as it is baked into immutable patterns as well:

But I think these cases usually won't occur, so I still want to know: are there any methods available to tell Mathematica, "I know testing result of elements will be the same in each trial, SKIP the repeated testing!"?

###Some Notes

  • I do NOT need a way to store the result of the pattern test, I'm trying to reduce the testing repetitions.

  • The latter one will speed up the simple pattern matching process in long lists considerably; the first one won't, as the pattern test completes in a flash, but there're are simply too many flashes, thus the overall speed is low.

  • Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can avoid the testing of the latter three 1, the whole process will be 4 times faster!

This is not simply a matter handing the somewhat unusual case of a stateful test function (like the one with flag) as it is baked into expressly stateless patterns as well:

added 1262 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can aviodavoid the testing of the latter three 1, the whole process will be 4 times faster!


Additional and related examples

Here is an additional example that may help illustrate the inefficiency of reapplying the same test to the same element.

Replace[
  {1.1, 2.2, 3, 4, 5},
  {a___, x__?((Print[##]; IntegerQ[#]) &), y__} :>
    {{a}, {x}, {y}}
]

1.1

1.1

1.1

1.1

2.2

2.2

2.2

3

{{1.1, 2.2}, {3}, {4, 5}}

This causes an algorithmic explosion in the time taken to process this pattern. In most applications it would be better if each element were only tested once. Observe that a test of a list of 200 elements performs almost twenty thousand tests:

i = 0;
MatchQ[N@Range[200], {a___, x__?((i++; IntegerQ[#]) &), y__}]
i
False

19900

This is not simply a matter handing the somewhat unusual case of a stateful test function (e.g. if the test function were (EvenQ[# + i++] &)) as it is baked into immutable patterns as well:

Needs["GeneralUtilities`"]

BenchmarkPlot[
  MatchQ[#, {a___, x__Integer, y__}] &,
  RandomReal[99, #] &
  , "IncludeFits" -> True
]

enter image description here

Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can aviod the testing of the latter three 1, the whole process will be 4 times faster!

Actually, the sample match I used as an example, the four tests complete in a flash, so memoizing will not speed things up. But if we can avoid the testing of the latter three 1, the whole process will be 4 times faster!


Additional and related examples

Here is an additional example that may help illustrate the inefficiency of reapplying the same test to the same element.

Replace[
  {1.1, 2.2, 3, 4, 5},
  {a___, x__?((Print[##]; IntegerQ[#]) &), y__} :>
    {{a}, {x}, {y}}
]

1.1

1.1

1.1

1.1

2.2

2.2

2.2

3

{{1.1, 2.2}, {3}, {4, 5}}

This causes an algorithmic explosion in the time taken to process this pattern. In most applications it would be better if each element were only tested once. Observe that a test of a list of 200 elements performs almost twenty thousand tests:

i = 0;
MatchQ[N@Range[200], {a___, x__?((i++; IntegerQ[#]) &), y__}]
i
False

19900

This is not simply a matter handing the somewhat unusual case of a stateful test function (e.g. if the test function were (EvenQ[# + i++] &)) as it is baked into immutable patterns as well:

Needs["GeneralUtilities`"]

BenchmarkPlot[
  MatchQ[#, {a___, x__Integer, y__}] &,
  RandomReal[99, #] &
  , "IncludeFits" -> True
]

enter image description here

correct link to point to the entire Q&A rather than a specific answer
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Made English more idiomatic
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Made English more idiomatic
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70
Loading