Performanz-orientierte Programmierung in C# Performanzaspekte von MS.NET Matej Ciesko Microsoft...
-
Upload
erna-doster -
Category
Documents
-
view
106 -
download
0
Transcript of Performanz-orientierte Programmierung in C# Performanzaspekte von MS.NET Matej Ciesko Microsoft...
Performanz-orientierte Programmierung Performanz-orientierte Programmierung in C#in C#
Performanzaspekte von MS.NETPerformanzaspekte von MS.NET
Matej Ciesko
Microsoft Student PartnerFriedrich – Alexander Universität Erlangen – Nürnberg
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
2 / 32
EinführungEinführung
Performanz-orientierte Programmierung• Optimierung
[Optimizing is the process of changing a program or the environment in which it runs to improve its performance.]
• Prozess der Optimierung:1. Problem identifizieren2. Problem evaluieren3. Anwenden einer Optimierungstechnik
AgendaAgenda
1. Einführung2. Performanzzähler (performance counters)
– Nanosekunden Timer– MS.NET Profiling API
3. MS.NET – Performanzaspekte– CLR, JIT, CG
4. Performanzaspekte bei Entwicklung mit C#– Objekte (GC)– Datentypen– Sicherer/Unsicherer Code– …
5. Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
4 / 32
ZieleZiele
1. Performanzaspekte des MS.NET
2. Messen von Performanzverhalten
3. Schreiben vom effizienten Code (C#)
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
5 / 32
EinführungEinführung
Beispielapplikation: Matrix – Matrix Multiplikation
C = A * B
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
6 / 32
Performanzaspekte von C#Performanzaspekte von C#
•ArraysArray[index]
Eliminierung der Überprüfung: Längentest im For-Header
for (int i=0; i < Array.Length; i++)
for (int i=0; i < Array.Length + y; i++) Array[i+6];
Überprüfung
sum += a[i]
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
7 / 32
Performanzaspekte von C#Performanzaspekte von C#
• C# Zeiger– Arbeiten außerhalb vom GC– „Pinnen“ von Objekten zum Speicher
unsafe{fixed (type* ptr = expr) statement
}
– Anmerkung: • Compiler flag: /unsafe• Code ist unsafe • Nach dem statement sind alle “pinned” Objekte für den GC wieder freigegeben
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
8 / 32
EinführungEinführung
Beispielapplikation:
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
9 / 32
ProfilingProfiling
Performanzzähler
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
10 / 32
ProfilingProfiling
• Fundamentale Prinzipien der Performanz-orientierten Programmierung:
1. Performanzziele setzen2. Messen, messen und noch mehr messen3. Optimieren
• Profiling: Präzise Messung (von Zeit/Clock Ticks) von Hardware und/Applikation um Aussagen über Laufzeitverhalten von Applikation treffen zu können.
1. Hardwarezähler (performance counters)2. Windows Performance Counters3. MS.NET Profiler API
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
11 / 32
ProfilingProfiling
• Hardware Counters• Sehr hohe zeitliche Auflösung!!!• Performanzereignisse
– Programmcharakteristika, Speicher, pipeline stalls, branch prediction, ressource utilization
• CPU Profiling Hardware – Ereignisdetektoren (event detectors)– Ereigniszähler (event counters)
Beispiel: RDTSC(Read Time Step Counter)
typedef struct _int32 { __int32 i32[2] } BigInt;BigInt start_ticks, end_tickes;
_asm {RDTSCmov start_ticks.int32val.i32[0], eaxmov start_ticks.int32val.i32[4], edx
}
• >2 x CPU
•SpeedStep
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
12 / 32
ProfilingProfiling
• Windows Performance Counters– MS Windows API– perfmon.exe
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
13 / 32
ProfilingProfiling
• .NET CLR Profiling Services• speziell für Verhalten von .NET
Applikationen• COM – Schnittstellen: ICorProfilerCallback
ICorProfilerInfo
• Profiler Design• COM Server Komponente implementieren• ICorProfilerCallback implementieren
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
14 / 32
ProfilingProfiling
• .NET CLR Profiling ServicesPerf. Counters MS.NET C# Zusammenfassung
CLR
_myProfiler
set Cor_Enable_Profiling = 0x1set Cor_Profiler={FA0C6E77-AFF4-…992C}
Cor_Enable_Profiling = TRUE
CoCreateInstance
ICorProfilerCallback::Initialize
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
15 / 32
ProfilingProfiling
• Allocation Profiler (Demo)Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
16 / 32
ProfilingProfiling
• Allocation Profiler
• Intel® VTune™ Performance Analyzer
• Rational® PurifyPlus for Windows
• Compuware® DevPartner Profiler
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
17 / 32
MS.NET PerformanzaspekteMS.NET Performanzaspekte
SpeicherverwaltungCompiler
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
18 / 32
.NET Performanzaspekte.NET Performanzaspekte
FortranFortranSource Source codecode
CompilerCompiler
C++C++C#C#
CompilerCompilerCompilerCompiler
AssemblyAssemblyIL CodeIL Code
AssemblyAssemblyIL CodeIL Code
AssemblyAssemblyIL CodeIL Code
Operating System ServicesOperating System Services
Common Language RuntimeCommon Language Runtime
JIT CompilerJIT Compiler
Native CodeNative Code
ManagedManagedcodecode
UnmanagedUnmanagedComponentComponent
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
19 / 32
.NET Performanzaspekte.NET Performanzaspekte
• JIT (Just-In-Time) Kompilierung
– Ausgangspunkt Plattformunabhängiger IL Code– Generelle Optimierung
• Constant folding, Method inlining, Loop unrolling, Common subexression elimination
– Optimierung zur Laufzeit• Lazy code selection, cache tuning, CPU spezifische Optimierung
– Theoretisch: Performanter als statische Compiler
– Manche Optimierungen aus Zeitgründen nicht möglich
– Längere „startup“ Zeiten der Applikationen• ngen.exe
+
-
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
20 / 32
.NET Performanzaspekte.NET Performanzaspekte
• SciMark Benchmark– Fast Fourier Transformations (FFT), Successive Over-Relaxation (SOR),
Monte-Carlo Quadrat, Sparse Matrix Multiplikation, LU Matrix Dekomposition
HW: 2.4GHz Pentium 4, 256 MB RAM
http://rotor.cs.cornell.edu/SciMark
1.0 1.1
0
100
200
300
MFLOP
v 1.1
+ 54.1 MFLOP
Architekturspez. Opt.
Double-to-int cast (IA-32 SSE2)
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
21 / 32
.NET Performanzaspekte.NET Performanzaspekte
• CLR – automatische Speicherverwaltung
[1]
„Allocation pattern“:
GUT: •Große Objekte, die am Programm- ende sterben•Kleine, kurzlebige Objekte
SCHLECHT:•Viele Objekte überleben bis in Gen 2 und sterben
•„alte“ Objekte referenzieren „junge“ Objekte
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
22 / 32
.NET Performanzaspekte.NET Performanzaspekte
• CLR – automatische Speicherverwaltung
– Maß für „Allocation Profile“:• Performance counter: .NET CLR Memory: %in GC
GUT < 30% SCHLECHT > 30%
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
23 / 32
.NET Performanzaspekte.NET Performanzaspekte
Microsoft Shared Source CLI - „Rotor“
• ECMA-334 (C#) • ECMA-335 (Common Language Infrastructure)• Microsoft Windows® XP• FreeBSD
SSCLI: www.sscli.net
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
24 / 32
Weitere C# TricksWeitere C# Tricks
GC-freundliches ProgrammierenBoxing, Werte/Referenztypen,
Zeiger, Loops
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
25 / 32
Performanzaspekte von C#Performanzaspekte von C#
GC & StringBuilder:Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
26 / 32
Performanzaspekte von C#Performanzaspekte von C#
Loops
foreach vs. for = Eleganz vs. Performanz
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
27 / 32
Performanzaspekte von C#Performanzaspekte von C#
SciMark
HW: 2.4GHz Pentium 4, 256 MB RAM
http://rotor.cs.cornell.edu/SciMark
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
28 / 32
ZusammenfassungZusammenfassung
• Profiling:– „Performance Counter“
• MS.NET – CLR, GC, JIT
• C# Optimierungen– GC-freundliches Programmieren– Boxing, Werte/Referenztypen, Zeiger,
Loops
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
29 / 32
ZusammenfassungZusammenfassung
• MS.NET als High Performance Plattform– Werkzeuge zur Performanzmessung und
Analyse– Ausdrucksstark– Einfach (Übersichtlich)– Effizienter Code– Plattformunabhängigkeit; Portabilität
Perf. Counters MS.NET C# Zusammenfassung
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
30 / 32
FragenFragen
Vielen Dank für die Aufmerksamkeit!
Haben Sie noch Fragen?
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
31 / 32
Wie geht es weiter auf der Wie geht es weiter auf der STC…?STC…?
Performanz-orientierte Programmierung in C#
STC 02. – 03. Juni 2004
32 / 32
RessourcenRessourcen
• www.studentconference.de• msdn.microsoft.com • www.microsoft.com/net• msnews.microsoft.com
• Rotor: www.sscli.net
[1] Performance Considerations for Run-Time Technologies in the .NET FrameworkA .NET Developer Platform White Paper; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp