Download presentation
Presentation is loading. Please wait.
Published byGilbert Thornton Modified over 9 years ago
1
When and How to Improve Code Performance? Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer http://Nikolay.IT
2
Computer Performance Code Tuning JustTrace C# Optimizations 2
4
Аn aspect of software quality that is important in human–computer interactions Resources: en.wikipedia.org/wiki/Computer_performance en.wikipedia.org/wiki/Computer_performance C#: http://www.dotnetperls.com/optimization http://www.dotnetperls.com/optimization Computer performance is characterized by the amount of useful work accomplished by a computer system compared to the time and resources used.
5
Good computer performance: Short response time for a given piece of work High throughput (rate of processing work) Low utilization of computing resource(s) High availability of the computing system or application Fast (or highly compact) data compression and decompression High bandwidth / short data transmission time 5
6
Example: “Vista's file copy performance is noticeably worse than Windows XP” – false: Vista uses algorithm that perform better in most cases Explorer waits 12 seconds before providing a copy duration estimate, which certainly provides no sense of smooth progress The copy dialog is not dismissed until the write- behind thread has committed the data to disk, which means the copy is slowest at the end 6
7
Performance improvements can reduce readability and complexity 7 “Premature optimization is the root of all evil.” Donald Knuth “More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason – including blind stupidity.” W. A. Wulf
8
Software requirements Software cost vs. performance System design Performance-oriented architecture Resource-reducing goals for individual subsystems, features, and classes Class and method design Data structures and algorithms 8
9
External Interactions Operating system External devices – storage, network, Internet Code Compilation / Code Execution Compiler optimizations Hardware Very often the cheapest way Code Tuning 9
11
What is code tuning / performance tuning? Modifying the code to make it run more efficiently (faster) Not the most effective / cheapest way to improve performance Often the code quality is decreased to increase the performance The 80 / 20 principle 20% of a program’s methods consume 80% of its execution time 11
12
Systematic code tuning follows these steps: 1.Assess the problem and establish numeric values that categorize acceptable behavior 2.Measure the performance of the system before modification 3.Identify the part of the system that is critical for improving the performance This is called the bottleneck 4.Modify that part of the system to remove the bottleneck 12
13
5.Measure the performance of the system after modification 6.If the modification makes the performance better, adopt it If the modification makes the performance worse, discard it 13
14
"Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code" false! 14 for i = 1 to 10 a[i] = i a[i] = i end for a[1] = 1 a[2] = 2 a[3] = 3 a[4] = 4 a[5] = 5 a[6] = 6 a[7] = 7 a[8] = 8 a[9] = 9 a[10] = 10 vs.
15
"A fast program is just as important as a correct one" false! The software should work correctly! 15
16
"Certain operations are probably faster or smaller than others" false! E.g. "add" is faster than "multiply" Always measure performance! "You should optimize as you go" false! It is hard to identify bottlenecks before a program is completely working Focus on optimization detracts from other program objectives Performance tuning breaks code quality! 16
17
Use a high-quality design Make the program right Make it modular and easily modifiable When it’s complete and correct, check the performance Consider compiler optimizations Measure, measure, measure Write clean code that’s easy to maintain Write use unit tests before optimizing 17
18
Junior developers think that "selection sort is slow"? Is this correct? Answer: depends! Think how many elements you sort Is "selection sort" slow for 20 or 50 elements? Is it slow for 1,000,000 elements? Shall we rewrite the sorting if we sort 20 elements? Conclusion: never optimize unless the piece of code is proven to be a bottleneck! 18
19
Measure to find bottlenecks Measurements need to be precise Measurements need to be repeatable 19
20
Measure improvement after each optimization If optimization does not improve performance revert it Stop testing when you know the answer 20
21
Resolve Performance Issues
22
What is JustTrace? A performance analysis tool Also called profiler Designed for code and memory profiling Measures the frequency and duration of function calls Traces the call stack Collects information about memory usage 22
23
Profiling and Improving Performance of "Mandelbrot Fractal" application (Code-Tuning-and-Optimization-Demo.zip) Code-Tuning-and-Optimization-Demo.zip
24
http://www.dotnetperls.com/optimization
25
Do We Need Optimizations? The C# language is fast (unlike Java) A bit slower than C and C++ Is it worthwhile to benchmark programming constructs? We should forget about small optimizations Say about 97% of the time: premature optimization is the root of all evil At all levels of performance optimization You should be taking measurements on the changes you make
26
26 Stopwatch measures time elapsed Useful for micro-benchmarks optimization using System.Diagnostics; static void Main() { Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); stopwatch.Start(); // Do something … // Do something … stopwatch.Stop(); stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);}
27
Static fields are faster than instance fields Instance methods are always slower than static methods To call an instance method, the instance reference must be resolved first Static methods do not use an instance reference It is faster to minimize method arguments Even use constants in the called methods instead of passing them arguments This causes less stack memory operations 27
28
When you call a method in your C# program The runtime allocates a separate memory region to store all local variable slots This memory is allocated on the stack Sometimes we can reuse the same variable Well-known anti-pattern for quality code Constants are fast Constants are not assigned a memory region, but are instead considered values Injected directly into the instruction stream 28
29
The switch statement compiles in a different way than if -statements typically do Some switches are faster than if -statements Using two-dimensional arrays is relatively slow We can explicitly create a one-dimensional array and access it through arithmetic The.NET Framework enables faster accesses to jagged arrays than to 2 D arrays Jagged arrays may cause slower garbage collections 29
30
StringBuilder can improve performance when appending strings Using char[] may be the fastest way to build up a string If you can store your data in an array of bytes, this allows you to save memory Smallest unit of addressable storage – byte Simple array T[] is always faster than List Simple array T[] is always faster than List Using efficient data structures (e.g. HashSet and Dictionary ) may speed-up the code 30
31
Use lazy evaluation (caching) for -loops are faster than foreach loops foreach uses enumerator C# structs are slower (in most cases) Structs are copied in their entirety on each function call or return value 31 public int GetSize() { if (this.size == null) if (this.size == null) size = CalculateSize(); size = CalculateSize(); return this.size; return this.size;}
32
Instead of testing each case using logic, you can translate it through a lookup table It eliminates costly branches in your code It is more efficient to work with a char instead of a single-char string Use JustDecompile or any other decompilation tool to view the output IL code Use Debug Windows Disassembly in VS Don’t do unnecessary optimizations! Measure after each change 32
33
33 string str = new string('a', 5000000); int count = 0; for (int i = 0; i < str.Length; i++) if (str[i] == 'a') if (str[i] == 'a') count++; count++; int count = 0; int len = str.Length; for (int i = 0; i < len; i++) if (str[i] == 'a') if (str[i] == 'a') count++; count++; int count = 0; foreach (var ch in str) if (ch == 'a') if (ch == 'a') count++; count++;
34
Manual or compiler optimization that replaces a method call with the method body You can manually paste a method body into its call spot or let the compiler to decide Typically, inlined code improves performance in micro-benchmarks … but makes the code hard to maintain! In.NET 4.5 you can force code inlining: 34 [MethodImpl(MethodImplOptions.AggressiveInlining)] void SomeMethod() { … }
35
Live Demo
36
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране http://academy.telerik.com
37
1. You are given a C# application (Code-Tuning-and- Optimization-Homework.zip) which displays an animated 3 D model of the Solar system. Code-Tuning-and- Optimization-Homework.zipCode-Tuning-and- Optimization-Homework.zip Use a profiler to find the places in its source code which cause significant performance degradation (bottlenecks). Provide a screenshot of the profiler’s result and indicate the place in the source code where the bottleneck resides (name of the file, line of code). Make a quick fix in the source code in order to significantly improve the performance. Test the code after the fix for correctness + performance. 37
38
2. Write a program to compare the performance of add, subtract, increment, multiply, divide for int, long, float, double and decimal values. 3. Write a program to compare the performance of square root, natural logarithm, sinus for float, double and decimal values. 4. * Write a program to compare the performance of insertion sort, selection sort, quicksort for int, double and string values. Check also the following cases: random values, sorted values, values sorted in reversed order. 38
39
C# Programming @ Telerik Academy csharpfundamentals.telerik.com csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com forums.academy.telerik.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.