Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: declaring lexical variables in shortest scope: performance? (on Code Optimization and Performance References)

by eyepopslikeamosquito (Archbishop)
on Apr 01, 2020 at 07:56 UTC ( [id://11114885]=note: print w/replies, xml ) Need Help??


in reply to declaring lexical variables in shortest scope: performance?

Don't diddle code to make it faster -- find a better algorithm

-- The Elements of Programming Style by Kernighan and Plauger

Don't Optimize Code -- Benchmark It

-- Ten Essential Development Practices by Damian Conway

It's important to be realistic: most people don't care about program performance most of the time

-- The Computer Language Benchmarks Game

The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming

-- Donald Knuth

Don't pessimize prematurely. All other things being equal, notably code complexity and readability, certain efficient design patterns and coding idioms should just flow naturally from your fingertips and are no harder to write than the pessimized alternatives. This is not premature optimization; it is avoiding gratuitous pessimization.

-- Andrei Alexandrescu and Herb Sutter

Rule 1: Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you have proven that's where the bottleneck is.
Rule 2: Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)
Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.
Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
Note: Pike's rules 1 and 2 restate Tony Hoare's "Premature optimization is the root of all evil". Ken Thompson rephrased Pike's rules 3 and 4 as "When in doubt, use brute force". Rules 3 and 4 are instances of the KISS principle. Rule 5 was stated by Fred Brooks in The Mythical Man-Month and is often shortened to "write stupid code that uses smart objects" (see also data structures vs code).

-- Rob Pike

If you do need to improve runtime you should try to be smart rather than clever. A smart solution is likely to approach the problem from an entirely different direction. A clever solution is likely to be a nasty mess arrived at by iterative tweaking the code to squeeze the last drop of performance out of a sub-optimum algorithm.

-- GrandFather replying here

Without good design, good algorithms, and complete understanding of the program's operation, your carefully optimized code will amount to one of mankind's least fruitful creations -- a fast slow program.

-- Michael Abrash

A couple of related general guidelines from On Coding Standards and Code Reviews:

  • Correctness, simplicity and clarity come first. Avoid unnecessary cleverness. If you must rely on cleverness, encapsulate and comment it.
  • Don't optimize prematurely. Benchmark before you optimize. Comment why you are optimizing.

On Interfaces and APIs cautions that library interfaces are very difficult to change once they become widely used - a fundamentally inefficient interface cannot be easily fixed later by optimizing. So it is not "premature optimization" to consider efficiency when designing public library interfaces.

See Also

  • Re^2: More Betterer Game of Life : reduced running time from 1635 seconds to 17 seconds ... where tweaking the code, via a long series of micro-optimizations, reduced the running time from 1635 secs to 450 secs (3.6 times faster), while finding a better algorithm reduced it from 450 secs to 17 secs (26.5 times faster)
  • Re^2: What's Perl good at or better than Python (Game of Life, LLiL, Rosetta and Performance References) : The C++ version of the simple GoL algorithm was 450/36 = 12.5 times faster than the Perl version; for the complex algorithm C++ was 17/0.08 = 212.5 times faster; C++ memory use was 2.8 times lower than Perl for the simple algorithm, 10.1 times lower for the complex one
  • Re: "my" cost by GrandFather : points to a youtube video where fierce competition in multiple languages improved the original running time of 32 days down to 0.006761 seconds (an improvement of 40,832,277,770%)

These experiences convinced me of don't assume measure and especially find a better algorithm!

Perl Performance References

High Performance and Parallel Computing References

  • VTune - Intel VTune, part of the Intel oneAPI Base Toolkit
  • Intel oneAPI - a unified API used across different computing accelerator (coprocessor) architectures, including GPUs, AI accelerators and field-programmable gate arrays

Extra Performance/Optimization References Added Later

Bitwise Operations:

Benchmark:

Database v Large Hash:

Other:

From BrowserUk (2012-2015):

From anonymonk (2023-2024):

From mldvx4 (2025):

Two spookily similar nodes posted late 2021 (both requesting XS C code, both by new monks who won't show us their code):

Some old classics:

Other:

On CPAN:

Some external references:

Mathematical:

Memory:

Sorting:

Multi-threading:

Compiler switches/flags:

I/O:

PDL and Array Processing References

See: Re^2: Organizational Culture (Part II): Meta Process (BioPerl/PDL/AI/Embedded/GPGPU/Data Science References)

RPerl References

See Also

Updated: Added Donald Knuth premature optimization quote and Alexandrescu/Sutter premature pessimization quotes and Rob Pike quotes. Mentioned efficiency of interfaces. Added more references.

  • Comment on Re: declaring lexical variables in shortest scope: performance? (on Code Optimization and Performance References)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: declaring lexical variables in shortest scope: performance?
by bliako (Abbot) on Apr 01, 2020 at 08:13 UTC

    fine with all these but if there is a long loop, then every little flop counts.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2026-04-14 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.