Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Schwartzian transform
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Efficiency analysis== Without the Schwartzian transform, the sorting in the example above would be written in Perl like this: <syntaxhighlight lang="perl"> @sorted = sort { foo($a) cmp foo($b) } @unsorted; </syntaxhighlight> While it is shorter to code, the naive approach here could be much less efficient if the key function (called {{mono|foo}} in the example above) is expensive to compute. This is because the code inside the brackets is evaluated each time two elements need to be compared. An optimal [[comparison sort]] performs ''[[big o notation|O]]''(''n log n'') comparisons (where ''n'' is the length of the list), with 2 calls to {{mono|foo}} every comparison, resulting in ''O''(''n log n'') calls to {{mono|foo}}. In comparison, using the Schwartzian transform, we only make 1 call to {{mono|foo}} per element, at the beginning {{mono|map}} stage, for a total of ''n'' calls to {{mono|foo}}. However, if the function {{mono|foo}} is relatively simple, then the extra overhead of the Schwartzian transform may be unwarranted.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)