在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → Pro .NET Performance-Optimize Your C# Applications.(2012)

Pro .NET Performance-Optimize Your C# Applications.(2012)

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:5.84M
  • 下载次数:16
  • 浏览次数:107
  • 发布时间:2022-12-23
  • 实例类别:常用C#方法
  • 发 布 人:ZSPINGGG
  • 文件格式:.pdf
  • 所需积分:2

实例介绍

【实例简介】Pro .NET Performance-Optimize Your C# Applications.(2012)

【实例截图】

【核心代码】

Contents
Foreword ......................................................................................................................xv
About the Authors .......................................................................................................xvii
About the Technical Reviewers ...................................................................................xix
Acknowledgments .......................................................................................................xxi
Introduction ...............................................................................................................xxiii
Chapter 1: Performance Metrics ■ .................................................................................1
Performance Goals ������������������������������������������������������������������������������������������������������������������2
Performance Metrics ���������������������������������������������������������������������������������������������������������������4
Summary ���������������������������������������������������������������������������������������������������������������������������������6
Chapter 2: Performance Measurement ■ ........................................................................7
Approaches to Performance Measurement �����������������������������������������������������������������������������7
Built-in Windows Tools ������������������������������������������������������������������������������������������������������������7
Performance Counters ������������������������������������������������������������������������������������������������������������������������������������8
Event Tracing for Windows (ETW) �����������������������������������������������������������������������������������������������������������������13
Time Profilers ������������������������������������������������������������������������������������������������������������������������26
Visual Studio Sampling Profiler ��������������������������������������������������������������������������������������������������������������������26
Visual Studio Instrumentation Profiler ����������������������������������������������������������������������������������������������������������31
Advanced Uses of Time Profilers ������������������������������������������������������������������������������������������������������������������33
Allocation Profilers ����������������������������������������������������������������������������������������������������������������35
Visual Studio Allocation Profiler ��������������������������������������������������������������������������������������������������������������������35
CLR Profiler ���������������������������������������������������������������������������������������������������������������������������������������������������39
Memory Profilers �������������������������������������������������������������������������������������������������������������������44
ANTS Memory Profiler ����������������������������������������������������������������������������������������������������������������������������������44
■ Contents
viii
SciTech �NET Memory Profiler ����������������������������������������������������������������������������������������������������������������������47
Other Profilers �����������������������������������������������������������������������������������������������������������������������49
Database and Data Access Profilers �������������������������������������������������������������������������������������������������������������49
Concurrency Profilers �����������������������������������������������������������������������������������������������������������������������������������50
I/O Profilers ���������������������������������������������������������������������������������������������������������������������������������������������������52
Microbenchmarking ��������������������������������������������������������������������������������������������������������������53
Poor Microbenchmark Example ��������������������������������������������������������������������������������������������������������������������54
Microbenchmarking Guidelines ��������������������������������������������������������������������������������������������������������������������56
Summary �������������������������������������������������������������������������������������������������������������������������������58
Chapter 3: Type Internals ■ ..........................................................................................61
An Example ���������������������������������������������������������������������������������������������������������������������������61
Semantic Differences between Reference Types and Value Types ���������������������������������������62
Storage, Allocation, and Deallocation ������������������������������������������������������������������������������������63
Reference Type Internals �������������������������������������������������������������������������������������������������������65
The Method Table �����������������������������������������������������������������������������������������������������������������������������������������66
Invoking Methods on Reference Type Instances�������������������������������������������������������������������������������������������70
Sync Blocks And The lock Keyword ������������������������������������������������������������������������������������������������������������75
Value Type Internals ��������������������������������������������������������������������������������������������������������������79
Value Type Limitations ����������������������������������������������������������������������������������������������������������������������������������80
Virtual Methods on Value Types ��������������������������������������������������������������������������������������������������������������������82
Boxing �����������������������������������������������������������������������������������������������������������������������������������������������������������82
Avoiding Boxing on Value Types with the Equals Method ���������������������������������������������������������������������������84
The GetHashCode Method ���������������������������������������������������������������������������������������������������������������������������87
Best Practices for Using Value Types ������������������������������������������������������������������������������������89
Summary �������������������������������������������������������������������������������������������������������������������������������89
Chapter 4: Garbage Collection ■ ...................................................................................91
Why Garbage Collection? ������������������������������������������������������������������������������������������������������91
Free List Management ����������������������������������������������������������������������������������������������������������������������������������92
Reference-Counting Garbage Collection �������������������������������������������������������������������������������������������������������93
Tracing Garbage Collection����������������������������������������������������������������������������������������������������94
■ Contents
ix
Mark Phase ���������������������������������������������������������������������������������������������������������������������������������������������������95
Sweep and Compact Phases ������������������������������������������������������������������������������������������������������������������������99
Pinning ��������������������������������������������������������������������������������������������������������������������������������������������������������102
Garbage Collection Flavors ��������������������������������������������������������������������������������������������������103
Pausing Threads for Garbage Collection �����������������������������������������������������������������������������������������������������103
Workstation GC �������������������������������������������������������������������������������������������������������������������������������������������106
Server GC ����������������������������������������������������������������������������������������������������������������������������������������������������107
Switching Between GC Flavors �������������������������������������������������������������������������������������������������������������������108
Generations �������������������������������������������������������������������������������������������������������������������������110
Generational Model Assumptions ���������������������������������������������������������������������������������������������������������������110
�NET Implementation of Generations ����������������������������������������������������������������������������������������������������������111
Large Object Heap ��������������������������������������������������������������������������������������������������������������������������������������115
References between Generations ���������������������������������������������������������������������������������������������������������������116
Background GC �������������������������������������������������������������������������������������������������������������������������������������������119
GC Segments and Virtual Memory ���������������������������������������������������������������������������������������119
Finalization ��������������������������������������������������������������������������������������������������������������������������123
Manual Deterministic Finalization ��������������������������������������������������������������������������������������������������������������123
Automatic Non-Deterministic Finalization ��������������������������������������������������������������������������������������������������123
Pitfalls of Non-Deterministic Finalization ���������������������������������������������������������������������������������������������������125
The Dispose Pattern �����������������������������������������������������������������������������������������������������������������������������������128
Weak References ����������������������������������������������������������������������������������������������������������������130
Interacting with the Garbage Collector ��������������������������������������������������������������������������������132
The System�GC Class ����������������������������������������������������������������������������������������������������������������������������������132
Interacting with the GC using CLR Hosting �������������������������������������������������������������������������������������������������135
GC Triggers �������������������������������������������������������������������������������������������������������������������������������������������������136
Garbage Collection Performance Best Practices �����������������������������������������������������������������137
Generational Model �������������������������������������������������������������������������������������������������������������������������������������137
Pinning ��������������������������������������������������������������������������������������������������������������������������������������������������������138
Finalization �������������������������������������������������������������������������������������������������������������������������������������������������139
Miscellaneous Tips and Best Practices ������������������������������������������������������������������������������������������������������139
Summary �����������������������������������������������������������������������������������������������������������������������������143
■ Contents
x
Chapter 5: Collections and Generics ■ ........................................................................145
Generics ������������������������������������������������������������������������������������������������������������������������������145
�NET Generics ���������������������������������������������������������������������������������������������������������������������������������������������148
Generic Constraints ������������������������������������������������������������������������������������������������������������������������������������149
Implementation of CLR Generics ����������������������������������������������������������������������������������������������������������������151
Collections ���������������������������������������������������������������������������������������������������������������������������158
Concurrent Collections �������������������������������������������������������������������������������������������������������������������������������160
Cache Considerations ���������������������������������������������������������������������������������������������������������������������������������162
Custom Collections ��������������������������������������������������������������������������������������������������������������166
Disjoint-Set (Union-Find) ����������������������������������������������������������������������������������������������������������������������������166
Skip List ������������������������������������������������������������������������������������������������������������������������������������������������������168
One-Shot Collections ����������������������������������������������������������������������������������������������������������������������������������169
Summary �����������������������������������������������������������������������������������������������������������������������������171
Chapter 6: Concurrency and Parallelism ■ .................................................................173
Challenges and Gains ����������������������������������������������������������������������������������������������������������173
Why Concurrency and Parallelism? ������������������������������������������������������������������������������������������������������������174
From Threads to Thread Pool to Tasks ��������������������������������������������������������������������������������174
Task Parallelism ������������������������������������������������������������������������������������������������������������������������������������������180
Data Parallelism �����������������������������������������������������������������������������������������������������������������������������������������186
C# 5 Async Methods �����������������������������������������������������������������������������������������������������������������������������������190
Advanced Patterns in the TPL ���������������������������������������������������������������������������������������������������������������������193
Synchronization �������������������������������������������������������������������������������������������������������������������194
Lock-Free Code ������������������������������������������������������������������������������������������������������������������������������������������195
Windows Synchronization Mechanisms �����������������������������������������������������������������������������������������������������200
Cache Considerations ���������������������������������������������������������������������������������������������������������������������������������202
General Purpose GPU Computing ����������������������������������������������������������������������������������������205
Introduction to C AMP ����������������������������������������������������������������������������������������������������������������������������205
Matrix Multiplication �����������������������������������������������������������������������������������������������������������������������������������207
N-Body Simulation ��������������������������������������������������������������������������������������������������������������������������������������208
Tiles and Shared Memory ���������������������������������������������������������������������������������������������������������������������������209
Summary �����������������������������������������������������������������������������������������������������������������������������213
■ Contents
xi
■Chapter 7: Networking, I/O, and Serialization ........................215 General I/O Concepts �
............................................................................................................................................215
Synchronous and Asynchronous I/O �................................................................................................................215
I/O Completion Ports �........................................................................................................................................217
NET Thread Pool �..............................................................................................................................................220
Copying Memory �..............................................................................................................................................221
Scatter–Gather I/O �..............................................................................................................221
File I/O �................................................................................................................................222
Cache Hinting �..................................................................................................................................................222
Unbuffered I/O �.................................................................................................................................................223
Networking �.........................................................................................................................223
Network Protocols �...........................................................................................................................................223
Network Sockets �.............................................................................................................................................225
Data Serialization and Deserialization �................................................................................226
Serializer Benchmarks �....................................................................................................................................226
DataSet Serialization �.......................................................................................................................................229
Windows Communication Foundation �................................................................................229
Throttling �.........................................................................................................................................................229
Process Model �.................................................................................................................................................230
Caching �............................................................................................................................................................231
Asynchronous WCF Clients and Servers �..........................................................................................................231
Bindings �...........................................................................................................................................................233
Summary �............................................................................................................................234
■Chapter 8: Unsafe Code and Interoperability ......................................235 Unsafe Code �
............................................................................................................................................235
Pinning and GC Handles �............................................................................................................................236
Lifetime Management �................................................................................................................................237
Allocating Unmanaged Memory �................................................................................................................237
Memory Pooling �.........................................................................................................................................238
■ Contents
xii
P/Invoke ������������������������������������������������������������������������������������������������������������������������������239
PInvoke�net and P/Invoke Interop Assistant ������������������������������������������������������������������������������������������������240
Binding �������������������������������������������������������������������������������������������������������������������������������������������������������241
Marshaler Stubs �����������������������������������������������������������������������������������������������������������������������������������������242
Blittable Types ��������������������������������������������������������������������������������������������������������������������������������������������245
Marshaling Direction, Value and Reference Types ��������������������������������������������������������������������������������������247
Code Access Security ���������������������������������������������������������������������������������������������������������������������������������248
COM Interoperability ������������������������������������������������������������������������������������������������������������248
Lifetime Management ���������������������������������������������������������������������������������������������������������������������������������249
Apartment Marshaling ��������������������������������������������������������������������������������������������������������������������������������249
TLB Import and Code Access Security ��������������������������������������������������������������������������������������������������������251
NoPIA ����������������������������������������������������������������������������������������������������������������������������������������������������������251
Exceptions ��������������������������������������������������������������������������������������������������������������������������������������������������252
C /CLI Language Extensions ��������������������������������������������������������������������������������������������253
The marshal_as Helper Library ����������������������������������������������������������������������������������������������������������������255
IL Code vs� Native Code ������������������������������������������������������������������������������������������������������������������������������256
Windows 8 WinRT Interop ���������������������������������������������������������������������������������������������������256
Best Practices for Interop ����������������������������������������������������������������������������������������������������257
Summary �����������������������������������������������������������������������������������������������������������������������������258
Chapter 9: Algorithm Optimization ■ ..........................................................................259
Taxonomy of Complexity �����������������������������������������������������������������������������������������������������259
Big-Oh Notation ������������������������������������������������������������������������������������������������������������������������������������������259
Turing Machines and Complexity Classes ��������������������������������������������������������������������������������������������������261
Memoization and Dynamic Programming ���������������������������������������������������������������������������265
Edit Distance �����������������������������������������������������������������������������������������������������������������������������������������������266
All-Pairs-Shortest-Paths �����������������������������������������������������������������������������������������������������������������������������267
Approximation ���������������������������������������������������������������������������������������������������������������������269
Traveling Salesman ������������������������������������������������������������������������������������������������������������������������������������270
Maximum Cut ���������������������������������������������������������������������������������������������������������������������������������������������271
■ Contents
xiii
Probabilistic Algorithms ������������������������������������������������������������������������������������������������������271
Probabilistic Maximum Cut �������������������������������������������������������������������������������������������������������������������������271
Fermat Primality Test ����������������������������������������������������������������������������������������������������������������������������������272
Indexing and Compression ��������������������������������������������������������������������������������������������������272
Variable Length Encoding ���������������������������������������������������������������������������������������������������������������������������272
Index Compression �������������������������������������������������������������������������������������������������������������������������������������273
Summary �����������������������������������������������������������������������������������������������������������������������������274
Chapter 10: Performance Patterns ■ ..........................................................................277
JIT Compiler Optimizations �������������������������������������������������������������������������������������������������277
Standard Optimizations ������������������������������������������������������������������������������������������������������������������������������277
Method Inlining �������������������������������������������������������������������������������������������������������������������������������������������278
Range-Check Elimination ���������������������������������������������������������������������������������������������������������������������������279
Tail Call �������������������������������������������������������������������������������������������������������������������������������������������������������281
Startup Performance �����������������������������������������������������������������������������������������������������������283
Pre-JIT Compilation with NGen (Native Image Generator) ��������������������������������������������������������������������������284
Multi-Core Background JIT Compilation �����������������������������������������������������������������������������������������������������287
Image Packers ��������������������������������������������������������������������������������������������������������������������������������������������288
Managed Profile-Guided Optimization (MPGO) �������������������������������������������������������������������������������������������288
Miscellaneous Tips for Startup Performance����������������������������������������������������������������������������������������������289
Processor-Specific Optimization �����������������������������������������������������������������������������������������291
Single Instruction Multiple Data (SIMD) ������������������������������������������������������������������������������������������������������292
Instruction-Level Parallelism ����������������������������������������������������������������������������������������������������������������������294
Exceptions ���������������������������������������������������������������������������������������������������������������������������297
Reflection ����������������������������������������������������������������������������������������������������������������������������298
Code Generation ������������������������������������������������������������������������������������������������������������������298
Generating Code from Source ��������������������������������������������������������������������������������������������������������������������299
Generating Code Using Dynamic Lightweight Code Generation �����������������������������������������������������������������300
Summary �����������������������������������������������������������������������������������������������������������������������������304
Chapter 11: Web Application Performance ■ ..............................................................305
Testing the Performance of Web Applications ���������������������������������������������������������������������305
■ Contents
xiv
Visual Studio Web Performance Test and Load Test �����������������������������������������������������������������������������������306
HTTP Monitoring Tools ��������������������������������������������������������������������������������������������������������������������������������307
Web Analyzing Tools �����������������������������������������������������������������������������������������������������������������������������������308
Improving Web Performance on the Server ������������������������������������������������������������������������308
Cache Commonly Used Objects ������������������������������������������������������������������������������������������������������������������308
Using Asynchronous Pages, Modules, and Controllers �������������������������������������������������������������������������������310
Tweaking the ASP�NET Environment ������������������������������������������������������������������������������������313
Turn Off ASP�NET Tracing and Debugging ���������������������������������������������������������������������������������������������������313
Disable View State ��������������������������������������������������������������������������������������������������������������������������������������315
Server-Side Output Cache ��������������������������������������������������������������������������������������������������������������������������316
Pre-Compiling ASP�NET Applications ����������������������������������������������������������������������������������������������������������317
Fine-Tuning the ASP�NET Process Model ����������������������������������������������������������������������������������������������������318
Configuring IIS ���������������������������������������������������������������������������������������������������������������������319
Output Caching �������������������������������������������������������������������������������������������������������������������������������������������319
Application Pool Configuration �������������������������������������������������������������������������������������������������������������������321
Optimizing the Network �������������������������������������������������������������������������������������������������������323
Apply HTTP Caching Headers ���������������������������������������������������������������������������������������������������������������������323
Turn on IIS Compression �����������������������������������������������������������������������������������������������������������������������������326
Minification and Bundling ���������������������������������������������������������������������������������������������������������������������������328
Use Content Delivery Networks (CDNs) ������������������������������������������������������������������������������������������������������329
Scaling ASP�NET Applications ����������������������������������������������������������������������������������������������330
Scaling Out �������������������������������������������������������������������������������������������������������������������������������������������������331
ASP�NET Scaling Mechanisms ��������������������������������������������������������������������������������������������������������������������331
Scaling Out Pitfalls �������������������������������������������������������������������������������������������������������������������������������������332
Summary �����������������������������������������������������������������������������������������������������������������������������332
Index ��������������������������������������������������������������������������������������������������������������������������������������335

实例下载地址

Pro .NET Performance-Optimize Your C# Applications.(2012)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警