0% found this document useful (0 votes)
2 views

optimizing-c-code-line-by-line-part-2-slides

The document discusses optimizing C# code with a focus on performance implications of property assignments, permission checks for files, and the cost of exceptions. It emphasizes the importance of benchmarking and unit testing while comparing different data types and methods for creating datetime objects. The key takeaway is to simplify code and avoid unnecessary work for better performance.

Uploaded by

Oliver Castro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

optimizing-c-code-line-by-line-part-2-slides

The document discusses optimizing C# code with a focus on performance implications of property assignments, permission checks for files, and the cost of exceptions. It emphasizes the importance of benchmarking and unit testing while comparing different data types and methods for creating datetime objects. The key takeaway is to simplify code and avoid unnecessary work for better performance.

Uploaded by

Oliver Castro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Optimizing C# Code

Line-by-line,
Part II

Chris B. Behrens

@chrisbbehrens
Demo
Performance in assignment
Assigning to a property
Assigning to public variable
Talk about the implications of it
For properties simple enough
to be represented as fields,
representing them as fields
makes no performance
difference
Just use properties
Demo Two ways of checking permission for a file
One quick and dirty
One that is a more formal implementation
A third approach
Talk about what it means
This Is Cheating, in the Best Way

1. Exceptions are costly


2.Reading file acls is slow
3.Forward-caching slow work allows
you to cheat, performance-wise
Enforcing our benchmarks
Using unit tests
A quick method we’ll whip up
To extract the data we want
From our benchmark run
Before We Get Started

But something more


We could grep and
Our results are in specific and
capture the results
the console output structured is more
from the output…
useful

We’ll capture the We’re going to just


result of the touch on this
Runner.Run() deeper subject of
command the API
Thoughts on Benchmark Testing

Running the benchmarks Run unit tests and


takes a while benchmarks in parallel
Demo
Compare six data types
Long, int, and byte
Float, double, and decimal
The principle for all of this
Be careful with the
decimal type
Demo Two ways to create datetime objects
Parsing from a string
Creating directly
Review the results
A middle ground between the two
Another Habit to Install

Are we comparing apples to


Yes, we are
oranges?
Do what you need to,
and nothing more
Use the simplest data type possible

Wrap-up If you can avoid doing work, you should


If you can do work ahead of time, you
should
When it comes to performance tips on the
Internet, trust, but verify…
And above all…
An ounce of test is worth a pound of guess

You might also like