11
$\begingroup$

Since this code work well:

Needs["GeneralUtilities`"]
GeneralUtilities`BenchmarkPlot[{SortBy[#, Identity] &, Sort}, Range, 
 "IncludeFits" -> True, TimeConstraint -> 8]

I want to set "IncludeFits" -> True, TimeConstraint -> 8 in every time to use BenchmarkPlot.So I want to make a private function to meet this need,and I hope Options[myBen] will give all option.

First Try

Options[myBen1] = {"IncludeFits" -> True, TimeConstraint -> 8};
myBen1[x__, opt : OptionsPattern[]] := 
 GeneralUtilities`BenchmarkPlot[x, opt]
myBen1[{SortBy[#, Identity] &, Sort}, Range]

Second Try

SetOptions[myBen2, {"IncludeFits" -> True, TimeConstraint -> 8}];
myBen2 := GeneralUtilities`BenchmarkPlot
myBen2[{SortBy[#, Identity] &, Sort}, Range]

As you see,the two option don't work both in this two tries.What's fault on my code?


PS: Options[GeneralUtilities`BenchmarkPlot] don't include the option of TimeConstraint.But it indeed take effect in fact.

$\endgroup$
13
$\begingroup$

New analysis (in v10.1)

For your newly stated goal:

If I don't want to build a new function.I just wanna change the GeneralUtilities`BenchmarkPlot's default option into {"IncludeFits" -> True, "Models" -> Automatic, TimeConstraint -> .8}. ... Can it be implemented?

The standard options model in Mathematica means that this should work:

Options[BenchmarkPlot] =
  {TimeConstraint -> 8, MaxIterations -> 1024, 
   "IncludeFits" -> True, "Models" -> Automatic};

It does not however. That means that BenchmarkPlot or one of its subordinate functions is not written correctly. Let's have a look. Using PrintDefinitions from the same package we find that our code will call

GeneralUtilities`Benchmarking`PackagePrivate`plot

Which has the definition (contexts omitted):

plot[data_Association, opts___Rule] := 
  ListLogLogPlot[
   Sequence @@ 
    addfits[data,
     Lookup[{opts}, "Models", Automatic], 
     MemberQ[{opts}, "IncludeFits" -> True]],
   FilterOptions[opts], 
   PlotLegends -> fstyle[Keys[data]], GridLines -> Automatic, 
   PlotMarkers -> {$pmarker}, GridLinesStyle -> Opacity[0.05`], 
   AxesLabel -> {"n", "time (s)"}, ImageSize -> Medium, PlotRangeClipping -> True, 
   Mesh -> False, 
   PlotRange -> {Full, 
     With[{v = Values[data][[All, All, 2]]}, {LogFloor[Min[v]]*1.1`^(-1), 
       LogCeiling[Max[v]]*1.1`}]}];

Please draw your attention to the line MemberQ[{opts}, "IncludeFits" -> True] and note that here opts is the pattern that will match explicit options given to this function and nothing else. BenchmarkPlot does not pass all options to this function either. This means that only explicit appearences of "IncludeFits" will have effect. (Likewise for "Models" from the line above.)

To correct this we either need to expressly pass all of Options[BenchmarkPlot] to this inner plot function, or we need to modify plot so that it considers Options[BenchmarkPlot]. I'll do the latter.

<< "GeneralUtilities`"

With[{plot = GeneralUtilities`Benchmarking`PackagePrivate`plot},
  PrependTo[DownValues[plot],
    HoldPattern[plot[arg___]] :> 
      Block[{$fixGUBPopts = True}, 
        plot[arg, Sequence @@ Options[BenchmarkPlot]]
      ] /; ! TrueQ[$fixGUBPopts]
  ]
];

Options[BenchmarkPlot] =
  {TimeConstraint -> 8, MaxIterations -> 1024,
   "IncludeFits" -> True, "Models" -> Automatic};

BenchmarkPlot[{SortBy[#, Identity] &, Sort}, Range]

enter image description here


Old answer

I wrote a fairly lengthy answer hoping to guide just this sort of thing; please take a look:

From your comment

But Options[myBen4] get {}. I hope it can get all options. Can it be implement?

I think you want something like this.

Options[myBen] = {"IncludeFits" -> True, "Models" -> Automatic, 
   TimeConstraint -> .8};

myBen[args__, opts : OptionsPattern[]] := 
 BenchmarkPlot[args, opts, Sequence @@ Options[myBen]]

myBen[{SortBy[#, Identity] &, Sort}, Range]


(source: clouddn.com)

And it meet the extra need:

Options[myBen]

{"IncludeFits"->True,Models->Automatic,TimeConstraint->0.8}

$\endgroup$
  • 1
    $\begingroup$ In version 11, TimeConstraint has gone inexplicably missing from Options[BenchmarkPlot]. TimeConstraint is still recognized internally by the implementation. MaxIterations is missing as well, but I don't that option was ever actually implemented, even in V10.1. $\endgroup$ – WReach Feb 19 '17 at 7:15
  • 1
    $\begingroup$ I make a edit for fit my case.It's seem I just omit that Sequence @@ Options[myBen].Thanks your answer very very much. :) $\endgroup$ – yode Feb 19 '17 at 7:20
  • 1
    $\begingroup$ It's work for you?I have tried it before.Look here. $\endgroup$ – yode Feb 19 '17 at 7:52
  • 2
    $\begingroup$ In V11.0.1, plot is in a different internal package. The patch needs to be applied to GeneralUtilities`Performance`PackagePrivate`plot. With that change, it works as advertised. $\endgroup$ – WReach Feb 19 '17 at 15:44
  • 2
    $\begingroup$ The original patch works up to V10.3.1. The updated patch is needed from V10.4 onward. $\endgroup$ – WReach Feb 19 '17 at 15:50
4
$\begingroup$

Including the options in the definition of your auxiliary function seems to work fine:

myBen3[x__, opt : OptionsPattern[]] := 
  GeneralUtilities`BenchmarkPlot[x, opt, "IncludeFits" -> True]

Above I left off the TimeConstraint option from the definition on purpose, because I wanted to showcase the fact that the helper function still correctly passes on extra options to BenchmarkPlot. Compare for instance:

myBen3[{SortBy[#, Identity] &, Sort}, Range, TimeConstraint -> 0.3]
myBen3[{SortBy[#, Identity] &, Sort}, Range]

Mathematica graphics

Mathematica graphics


So a more complete definition would be:

myBen4[x__, opt : OptionsPattern[]] :=
 GeneralUtilities`BenchmarkPlot[x, opt, "IncludeFits" -> True, TimeConstraint -> 8]
$\endgroup$
  • $\begingroup$ But Options[myBen4] get {}.I hope to get all option.Can it be implement? $\endgroup$ – yode Feb 19 '17 at 5:50
  • $\begingroup$ If not,I can do it just by myBen := GeneralUtilities`BenchmarkPlot[##, "IncludeFits" -> True, TimeConstraint -> 8] & $\endgroup$ – yode Feb 19 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.