SlideShare une entreprise Scribd logo
1  sur  39
The “Evils” of OptimizationOr: Performance Anxiety Can Cause Premature Optimization James Hare – Application Architect, Scottrade
What is Optimization? Definition from freedictionary.com: “The procedure or procedures used to make a system or design as effective or functional as possible, especially the mathematical techniques involved”	 This is a general term applied to the optimization of any process or system. What does it mean for us?
Ada Lovelace “In almost every computation a great variety of arrangements for the succession of the processes is possible, and various considerations must influence the selection amongst them for the purposes of a Calculating Engine. One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation.” - Ada Byron’s Notes on Charles Babbage's Analytical Engine, 1842
Software Optimization Software can be optimized in several areas: In the design of system Choice of appropriate techniques and structures. In the code that implements the system Choice of logic that implements a feature. In the compiler that builds the system Improvements the compiler bakes into assemblies. In the runtime that executes the system Choices the CLR/JIT can make in executing assemblies.
So Optimization is Good, Right? Well, yes and no… Yes: Time optimizing design is nearly always well spent. Optimizing a known bottleneck will increase speed. Compiler and CLR optimizations are already there and do not impact readability or maintainability. No: “Slower” is nearly always better than wrong. Optimizing code before you have a measured need can quickly become an anti-pattern.
William Wulf “More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason – including blind stupidity.” "A Case Against the GOTO," Proceedings of the 25th National ACM Conference, August 1972, pp. 791-97.
Is an Anti-Pattern a Design Pattern? No, most of us are familiar with the term Design Pattern: A general, reusable solution to a commonly occurring problem in software design. Not a finished design, but a general description or template for how to solve a problem. Became very popular as a concept after the book Design Patterns: Elements of Reusable Object-Oriented Software released in 1994 by the “Gang of Four”.
Okay, So What’s an Anti-Pattern? It is the antithesis of a Design Pattern. It is a pattern that tends to be commonly used, but which usually turns out to be ineffective and/or counterproductive. Term was coined by Andrew Koenig in 1995 in his article Patterns and Antipatterns. Made popular in 1999 with the book AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis.
How Do Anti-Patterns Relate to Optimization? Anti-Patterns cover a wide range from: Organizational (Analysis Paralysis, etc.) Project Management (Death March, etc.) Analysis (Bystander Apathy) Software Design (Big Ball of Mud, etc.) Object-Oriented Design (God Object, etc.) Programming (Spaghetti Coder, etc.) Configuration Management (Dependency Hell, etc.) Methodological (Premature-Optimization, etc.)
Donald Knuth “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.  A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified” “Structured Programming with Goto Statements”,  Computing Surveys6:4 (1974), 261–301.
Premature Optimization? Performance anxiety disproportionately affects the design of a piece of code. Oftentimes in premature optimization, the developer has no measurable data to show the code needs optimization. This can result in code optimizations that have no net impact on overall performance. Many times this can happen by misapplied optimization “rules-of-thumb.”
Premature Optimization Can Introduce Bugs Code may become overly complicated since programmer is distracted by the perceived need to optimize. Bugs may be immediately introduced during an incorrect optimization. Since code becomes less maintainable, future enhancements are more likely to cause bugs. Remember: A slightly slower program is better than an erroneous program.
Premature Optimization CanBe a Waste of Time Remember Knuth: Ignore small efficiency gains 97% of the time, may pay attention to the 3% after they have been identified. Optimizing code that is not a bottleneck is almost always a waste of time. Slows the entire process for little benefit, or worse yet may add maintenance costs or bugs. Correct code delivered faster is often better than fastest incorrect code delivered slowly!
Rob Pike “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.”  “Notes on Programming in C”, Feburary 21, 1989
So Is Optimization Always Bad? Not at all, only optimizing for the sake of optimizing is bad. Good places for optimization are: Early in system and component design. When you find measurable bottlenecks. In those rare, but necessary, “real-time” systems. When your group has “standard” optimizations.
Best Place to Catch Bottlenecks? source: http://en.wikipedia.org/wiki/Waterfall_model
Design Optimizations Most system bottlenecks are from bad design. When designing systems: Use multithreading correctly when appropriate. Use best algorithms/collections for the problem. Avoid designing a single component/process that will become a bottleneck to the whole system. Choose the best communication protocol and methodology for the problem. Prefer to design web methods to be chunky. Cache infrequently changing remote data.
Design: Multi-Threading Wisely Use the .NET 4.0 Concurrent Collections wherever appropriate. Strongly consider the .NET 4.0 TPL over traditional multi-threading. Use locking judiciously, keep locks as small in scope as possible and avoid holding multiple locks at same time. Avoid serial thread processing (handing off from bucket to bucket to bucket…)
Design: Use Best Algorithms Favor keeping algorithmic complexity low:
Design: Use Best Algorithms Know and use your LINQ algorithms: Finds: Any(), First(), Find(), etc. Queries: Select(), Where(), etc. Grouping: GroupBy() Sorting: OrderBy() Etc. Already written, optimized, and unit tested. Can make code much easier to read and maintain.
Design: Use Best Algorithms
Design: Use Best Collection Know each collection’s strengths and weaknesses. In general choose based on: Ordering: Do you need to maintain order? Lookup: Is fast lookup the ultimate goal? Insert/Deletes: Where are inserts/deletes performed?  Are they needed? Synchronization: Do you need multi-threaded access of a mutable collection?
Design: Avoid Bottlenecks If one process or component in a system has to serialize and process all items slowly, it doesn’t matter how parallel the rest of your system is.
Design: Use Best Communication Know best method for the problem: Do you need broadcast communication or not care if some packets get dropped?  Consider UDP. Do you need connection-oriented reliable synchronous communication?  Consider TCP. Do you need to be able to process messages asynchronously?  Consider message queues. Do you need to be able to make cross-platform calls?   Consider web methods or message queues.
Design: Web Methods Good OO design ≠ good distributed design. Prefer “chunky” to “chatty” web methods as they will have less network overhead. “chunky”: one method returns all data needed. “chatty”: many small methods that return pieces. Do not return DataSet! Very, very, very large! Return array or List<T> of custom type instead.
Design: Caching When remote data never changes, cache it! Can use unsynchronized Dictionary. Much faster than network lookup. When remote data changes infrequently, cache with expiration or refresh. Consider ConcurrentDictionary. In ASP.NET use Application and Session caches. Consider distributed cache when appropriate. AppFabric (Velocity), Coherence, etc.
Measurable Bottlenecks Most bottlenecks can be avoided in design. However, some bottlenecks only become apparent after testing or possibly even later in production after user or data size growth. Measure, Improve, and Re-Measure: Don’t guess at location or cause ever! Profile the code to find the bottleneck. Solve the revealed bottleneck and document. Re-profile to make sure code is streamlined.
Bottleneck in Lock Contention
After Removing Unnecessary Lock
“Real-time” Systems Obviously, there are some rare cases where performance is paramount: Flight control systems. Algorithmic trading. Gaming. In these cases, you can always code with an eye towards performance. The decisions on how to optimize, though, should be well known and uniform for group.
“Standard” Optimizations These are a double-edged sword because they can often be misapplied or incorrectly understood. Framework changes may alter the efficiency of an operation/method to where net gain is nil. Any such optimizations should be decided on as a group and standardized. In general, these should be avoided except for Microsoft recommended best practices.
Microsoft Performance Recommendations Throw fewer exceptions Make chunky calls Use value types for small immutable data Use AddRange() when possible Trim your Working Set Use for for string iteration (careful!) Use StringBuilder for complex String manipulation Use jagged arrays vs. rectangular arrays Etc.
“Standard” Misuse Example: Someone hears that StringBuilder is more efficient than concatenation and sacrifices readability for “performance”:
“Standard” Misuse String concatenation (+) is faster for single-concatenations with two or more arguments. Knows size of arguments in advance and computes correct buffer size. StringBuilder uses a default buffer size and then re-allocates if needs more space. String concatenation of literals is done at compile time and has no impact. Don’t apply “standard” rules without fully knowing the ramifications.
Summary Optimizing design and known bottlenecks is nearly always a beneficial activity. Sacrificing maintainability for unmeasured performance gain is problematic: Code that is harder to maintain is more likely to have initial bugs or bugs in modification. Most of the time micro-optimizations have no affect on overall system performance and just creates a time sink.
Michael A. Jackson “There are two rules for when to optimize:  Don't do it.  (For experts only) Don't do it yet."  “Principles of Program Design”, Academic Press, London and New York, 1975.
Questions? Blog: http://www.geekswithblogs.net/BlackRabbitCoder Email: james.michael.hare@gmail.com Twitter: http://twitter.com/BlkRabbitCoder
The "Evils" of Optimization

Contenu connexe

Tendances

Refactoring - An Introduction
Refactoring - An IntroductionRefactoring - An Introduction
Refactoring - An IntroductionGiorgio Vespucci
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practicesmh_azad
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문Sangcheol Hwang
 
ReSharper Presentation for NUGs
ReSharper Presentation for NUGsReSharper Presentation for NUGs
ReSharper Presentation for NUGsDmitri Nesteruk
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentJohn Stevenson
 
Principles in Refactoring
Principles in RefactoringPrinciples in Refactoring
Principles in RefactoringChamnap Chhorn
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NETBlackRabbitCoder
 
Extracts from "Clean code"
Extracts from "Clean code"Extracts from "Clean code"
Extracts from "Clean code"VlatkaPavii
 
Working Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeAmar Shah
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy CodeEyal Golan
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 

Tendances (20)

Refactoring - An Introduction
Refactoring - An IntroductionRefactoring - An Introduction
Refactoring - An Introduction
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practices
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
ReSharper Presentation for NUGs
ReSharper Presentation for NUGsReSharper Presentation for NUGs
ReSharper Presentation for NUGs
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
 
Principles in Refactoring
Principles in RefactoringPrinciples in Refactoring
Principles in Refactoring
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
 
Extracts from "Clean code"
Extracts from "Clean code"Extracts from "Clean code"
Extracts from "Clean code"
 
Working Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in Practice
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Working Effectively with Legacy Code
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Code
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 

En vedette

Mongo db model relationships with documents
Mongo db model relationships with documentsMongo db model relationships with documents
Mongo db model relationships with documentsDr. Awase Khirni Syed
 
On Becoming a Technical Lead
On Becoming a Technical LeadOn Becoming a Technical Lead
On Becoming a Technical LeadBuu Nguyen
 
Asp.Net Identity
Asp.Net IdentityAsp.Net Identity
Asp.Net IdentityMarwa Ahmad
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#Alfonso Garcia-Caro
 
Dynamic Binding in C# 4.0
Dynamic Binding in C# 4.0Dynamic Binding in C# 4.0
Dynamic Binding in C# 4.0Buu Nguyen
 
C# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTC# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTDr. Awase Khirni Syed
 

En vedette (10)

Api
ApiApi
Api
 
Mongo db model relationships with documents
Mongo db model relationships with documentsMongo db model relationships with documents
Mongo db model relationships with documents
 
On Becoming a Technical Lead
On Becoming a Technical LeadOn Becoming a Technical Lead
On Becoming a Technical Lead
 
Asp.Net Identity
Asp.Net IdentityAsp.Net Identity
Asp.Net Identity
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#
 
Es2015 training material-syedawase
Es2015 training material-syedawaseEs2015 training material-syedawase
Es2015 training material-syedawase
 
Dynamic Binding in C# 4.0
Dynamic Binding in C# 4.0Dynamic Binding in C# 4.0
Dynamic Binding in C# 4.0
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
C# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTC# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENT
 
C#/.NET Little Wonders
C#/.NET Little WondersC#/.NET Little Wonders
C#/.NET Little Wonders
 

Similaire à The "Evils" of Optimization

Pega robotics best practices building solutions (1)
Pega robotics best practices   building solutions (1)Pega robotics best practices   building solutions (1)
Pega robotics best practices building solutions (1)KPMG US
 
Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Santhoo Vardan
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsSteven Smith
 
Software Bugs A Software Architect Point Of View
Software Bugs    A Software Architect Point Of ViewSoftware Bugs    A Software Architect Point Of View
Software Bugs A Software Architect Point Of ViewShahzad
 
Secret Twists to Efficiently Develop Reactive Software Systems
Secret Twists to Efficiently Develop Reactive Software SystemsSecret Twists to Efficiently Develop Reactive Software Systems
Secret Twists to Efficiently Develop Reactive Software SystemsBart Jonkers
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tipsBill Buchan
 
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINAL
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINALJun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINAL
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINALAlex Tarra
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to considerLloydMoore
 
Agile Methodologies And Extreme Programming
Agile Methodologies And Extreme ProgrammingAgile Methodologies And Extreme Programming
Agile Methodologies And Extreme ProgrammingUtkarsh Khare
 
The View - 30 proven Lotuscript tips
The View - 30 proven Lotuscript tipsThe View - 30 proven Lotuscript tips
The View - 30 proven Lotuscript tipsBill Buchan
 
Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovSvetlin Nakov
 
Creating An Incremental Architecture For Your System
Creating An Incremental Architecture For Your SystemCreating An Incremental Architecture For Your System
Creating An Incremental Architecture For Your SystemGiovanni Asproni
 
Top 30 Scalability Mistakes
Top 30 Scalability MistakesTop 30 Scalability Mistakes
Top 30 Scalability MistakesJohn Coggeshall
 
Is your Automation Infrastructure ‘Well Architected’?
Is your Automation Infrastructure ‘Well Architected’?Is your Automation Infrastructure ‘Well Architected’?
Is your Automation Infrastructure ‘Well Architected’?Adam Goucher
 
Structured Software Design
Structured Software DesignStructured Software Design
Structured Software DesignGiorgio Zoppi
 
Software Development Standard Operating Procedure
Software Development Standard Operating Procedure Software Development Standard Operating Procedure
Software Development Standard Operating Procedure rupeshchanchal
 
Common Project Mistakes (And How to Avoid Them)
Common Project Mistakes (And How to Avoid Them)Common Project Mistakes (And How to Avoid Them)
Common Project Mistakes (And How to Avoid Them)Inductive Automation
 

Similaire à The "Evils" of Optimization (20)

Pega robotics best practices building solutions (1)
Pega robotics best practices   building solutions (1)Pega robotics best practices   building solutions (1)
Pega robotics best practices building solutions (1)
 
Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
251 - Alogarithms Lects.pdf
251 - Alogarithms Lects.pdf251 - Alogarithms Lects.pdf
251 - Alogarithms Lects.pdf
 
Software Bugs A Software Architect Point Of View
Software Bugs    A Software Architect Point Of ViewSoftware Bugs    A Software Architect Point Of View
Software Bugs A Software Architect Point Of View
 
Put to the Test
Put to the TestPut to the Test
Put to the Test
 
Secret Twists to Efficiently Develop Reactive Software Systems
Secret Twists to Efficiently Develop Reactive Software SystemsSecret Twists to Efficiently Develop Reactive Software Systems
Secret Twists to Efficiently Develop Reactive Software Systems
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINAL
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINALJun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINAL
Jun 08 - PMWT Featured Paper -Tarabykin - XP PAPER - FINAL
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to consider
 
Agile Methodologies And Extreme Programming
Agile Methodologies And Extreme ProgrammingAgile Methodologies And Extreme Programming
Agile Methodologies And Extreme Programming
 
The View - 30 proven Lotuscript tips
The View - 30 proven Lotuscript tipsThe View - 30 proven Lotuscript tips
The View - 30 proven Lotuscript tips
 
Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin Nakov
 
Creating An Incremental Architecture For Your System
Creating An Incremental Architecture For Your SystemCreating An Incremental Architecture For Your System
Creating An Incremental Architecture For Your System
 
Top 30 Scalability Mistakes
Top 30 Scalability MistakesTop 30 Scalability Mistakes
Top 30 Scalability Mistakes
 
Is your Automation Infrastructure ‘Well Architected’?
Is your Automation Infrastructure ‘Well Architected’?Is your Automation Infrastructure ‘Well Architected’?
Is your Automation Infrastructure ‘Well Architected’?
 
Quality Software Development
Quality Software DevelopmentQuality Software Development
Quality Software Development
 
Structured Software Design
Structured Software DesignStructured Software Design
Structured Software Design
 
Software Development Standard Operating Procedure
Software Development Standard Operating Procedure Software Development Standard Operating Procedure
Software Development Standard Operating Procedure
 
Common Project Mistakes (And How to Avoid Them)
Common Project Mistakes (And How to Avoid Them)Common Project Mistakes (And How to Avoid Them)
Common Project Mistakes (And How to Avoid Them)
 

The "Evils" of Optimization

  • 1. The “Evils” of OptimizationOr: Performance Anxiety Can Cause Premature Optimization James Hare – Application Architect, Scottrade
  • 2. What is Optimization? Definition from freedictionary.com: “The procedure or procedures used to make a system or design as effective or functional as possible, especially the mathematical techniques involved” This is a general term applied to the optimization of any process or system. What does it mean for us?
  • 3. Ada Lovelace “In almost every computation a great variety of arrangements for the succession of the processes is possible, and various considerations must influence the selection amongst them for the purposes of a Calculating Engine. One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation.” - Ada Byron’s Notes on Charles Babbage's Analytical Engine, 1842
  • 4. Software Optimization Software can be optimized in several areas: In the design of system Choice of appropriate techniques and structures. In the code that implements the system Choice of logic that implements a feature. In the compiler that builds the system Improvements the compiler bakes into assemblies. In the runtime that executes the system Choices the CLR/JIT can make in executing assemblies.
  • 5. So Optimization is Good, Right? Well, yes and no… Yes: Time optimizing design is nearly always well spent. Optimizing a known bottleneck will increase speed. Compiler and CLR optimizations are already there and do not impact readability or maintainability. No: “Slower” is nearly always better than wrong. Optimizing code before you have a measured need can quickly become an anti-pattern.
  • 6. William Wulf “More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason – including blind stupidity.” "A Case Against the GOTO," Proceedings of the 25th National ACM Conference, August 1972, pp. 791-97.
  • 7. Is an Anti-Pattern a Design Pattern? No, most of us are familiar with the term Design Pattern: A general, reusable solution to a commonly occurring problem in software design. Not a finished design, but a general description or template for how to solve a problem. Became very popular as a concept after the book Design Patterns: Elements of Reusable Object-Oriented Software released in 1994 by the “Gang of Four”.
  • 8. Okay, So What’s an Anti-Pattern? It is the antithesis of a Design Pattern. It is a pattern that tends to be commonly used, but which usually turns out to be ineffective and/or counterproductive. Term was coined by Andrew Koenig in 1995 in his article Patterns and Antipatterns. Made popular in 1999 with the book AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis.
  • 9. How Do Anti-Patterns Relate to Optimization? Anti-Patterns cover a wide range from: Organizational (Analysis Paralysis, etc.) Project Management (Death March, etc.) Analysis (Bystander Apathy) Software Design (Big Ball of Mud, etc.) Object-Oriented Design (God Object, etc.) Programming (Spaghetti Coder, etc.) Configuration Management (Dependency Hell, etc.) Methodological (Premature-Optimization, etc.)
  • 10. Donald Knuth “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified” “Structured Programming with Goto Statements”, Computing Surveys6:4 (1974), 261–301.
  • 11. Premature Optimization? Performance anxiety disproportionately affects the design of a piece of code. Oftentimes in premature optimization, the developer has no measurable data to show the code needs optimization. This can result in code optimizations that have no net impact on overall performance. Many times this can happen by misapplied optimization “rules-of-thumb.”
  • 12. Premature Optimization Can Introduce Bugs Code may become overly complicated since programmer is distracted by the perceived need to optimize. Bugs may be immediately introduced during an incorrect optimization. Since code becomes less maintainable, future enhancements are more likely to cause bugs. Remember: A slightly slower program is better than an erroneous program.
  • 13. Premature Optimization CanBe a Waste of Time Remember Knuth: Ignore small efficiency gains 97% of the time, may pay attention to the 3% after they have been identified. Optimizing code that is not a bottleneck is almost always a waste of time. Slows the entire process for little benefit, or worse yet may add maintenance costs or bugs. Correct code delivered faster is often better than fastest incorrect code delivered slowly!
  • 14. Rob Pike “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.” “Notes on Programming in C”, Feburary 21, 1989
  • 15. So Is Optimization Always Bad? Not at all, only optimizing for the sake of optimizing is bad. Good places for optimization are: Early in system and component design. When you find measurable bottlenecks. In those rare, but necessary, “real-time” systems. When your group has “standard” optimizations.
  • 16. Best Place to Catch Bottlenecks? source: http://en.wikipedia.org/wiki/Waterfall_model
  • 17. Design Optimizations Most system bottlenecks are from bad design. When designing systems: Use multithreading correctly when appropriate. Use best algorithms/collections for the problem. Avoid designing a single component/process that will become a bottleneck to the whole system. Choose the best communication protocol and methodology for the problem. Prefer to design web methods to be chunky. Cache infrequently changing remote data.
  • 18. Design: Multi-Threading Wisely Use the .NET 4.0 Concurrent Collections wherever appropriate. Strongly consider the .NET 4.0 TPL over traditional multi-threading. Use locking judiciously, keep locks as small in scope as possible and avoid holding multiple locks at same time. Avoid serial thread processing (handing off from bucket to bucket to bucket…)
  • 19. Design: Use Best Algorithms Favor keeping algorithmic complexity low:
  • 20. Design: Use Best Algorithms Know and use your LINQ algorithms: Finds: Any(), First(), Find(), etc. Queries: Select(), Where(), etc. Grouping: GroupBy() Sorting: OrderBy() Etc. Already written, optimized, and unit tested. Can make code much easier to read and maintain.
  • 21. Design: Use Best Algorithms
  • 22. Design: Use Best Collection Know each collection’s strengths and weaknesses. In general choose based on: Ordering: Do you need to maintain order? Lookup: Is fast lookup the ultimate goal? Insert/Deletes: Where are inserts/deletes performed? Are they needed? Synchronization: Do you need multi-threaded access of a mutable collection?
  • 23.
  • 24. Design: Avoid Bottlenecks If one process or component in a system has to serialize and process all items slowly, it doesn’t matter how parallel the rest of your system is.
  • 25. Design: Use Best Communication Know best method for the problem: Do you need broadcast communication or not care if some packets get dropped? Consider UDP. Do you need connection-oriented reliable synchronous communication? Consider TCP. Do you need to be able to process messages asynchronously? Consider message queues. Do you need to be able to make cross-platform calls? Consider web methods or message queues.
  • 26. Design: Web Methods Good OO design ≠ good distributed design. Prefer “chunky” to “chatty” web methods as they will have less network overhead. “chunky”: one method returns all data needed. “chatty”: many small methods that return pieces. Do not return DataSet! Very, very, very large! Return array or List<T> of custom type instead.
  • 27. Design: Caching When remote data never changes, cache it! Can use unsynchronized Dictionary. Much faster than network lookup. When remote data changes infrequently, cache with expiration or refresh. Consider ConcurrentDictionary. In ASP.NET use Application and Session caches. Consider distributed cache when appropriate. AppFabric (Velocity), Coherence, etc.
  • 28. Measurable Bottlenecks Most bottlenecks can be avoided in design. However, some bottlenecks only become apparent after testing or possibly even later in production after user or data size growth. Measure, Improve, and Re-Measure: Don’t guess at location or cause ever! Profile the code to find the bottleneck. Solve the revealed bottleneck and document. Re-profile to make sure code is streamlined.
  • 29. Bottleneck in Lock Contention
  • 31. “Real-time” Systems Obviously, there are some rare cases where performance is paramount: Flight control systems. Algorithmic trading. Gaming. In these cases, you can always code with an eye towards performance. The decisions on how to optimize, though, should be well known and uniform for group.
  • 32. “Standard” Optimizations These are a double-edged sword because they can often be misapplied or incorrectly understood. Framework changes may alter the efficiency of an operation/method to where net gain is nil. Any such optimizations should be decided on as a group and standardized. In general, these should be avoided except for Microsoft recommended best practices.
  • 33. Microsoft Performance Recommendations Throw fewer exceptions Make chunky calls Use value types for small immutable data Use AddRange() when possible Trim your Working Set Use for for string iteration (careful!) Use StringBuilder for complex String manipulation Use jagged arrays vs. rectangular arrays Etc.
  • 34. “Standard” Misuse Example: Someone hears that StringBuilder is more efficient than concatenation and sacrifices readability for “performance”:
  • 35. “Standard” Misuse String concatenation (+) is faster for single-concatenations with two or more arguments. Knows size of arguments in advance and computes correct buffer size. StringBuilder uses a default buffer size and then re-allocates if needs more space. String concatenation of literals is done at compile time and has no impact. Don’t apply “standard” rules without fully knowing the ramifications.
  • 36. Summary Optimizing design and known bottlenecks is nearly always a beneficial activity. Sacrificing maintainability for unmeasured performance gain is problematic: Code that is harder to maintain is more likely to have initial bugs or bugs in modification. Most of the time micro-optimizations have no affect on overall system performance and just creates a time sink.
  • 37. Michael A. Jackson “There are two rules for when to optimize: Don't do it. (For experts only) Don't do it yet." “Principles of Program Design”, Academic Press, London and New York, 1975.
  • 38. Questions? Blog: http://www.geekswithblogs.net/BlackRabbitCoder Email: james.michael.hare@gmail.com Twitter: http://twitter.com/BlkRabbitCoder

Notes de l'éditeur

  1. Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides
  2. William Brown, Raphael Malveau, Skip McCormick, and Tom Mowbray; with Scott Thomas
  3. http://msdn.microsoft.com/en-us/library/ms973839.aspx#dotnetperftips_topic2