Boxing & unboxing
-
Upload
larry-nung -
Category
Technology
-
view
65 -
download
0
Transcript of Boxing & unboxing
![Page 1: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/1.jpg)
BOXING & UNBOXING
Larry Nung
![Page 2: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/2.jpg)
AGENDA
Boxing/UnBoxing
Issue for boxing/unboxing
How to find out boxing/Unboxing
2
![Page 3: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/3.jpg)
BOXING/UNBOXING
3
![Page 4: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/4.jpg)
BOXING
Value Type => Object
New object must be allocated and constructed
Expensive computationally
4
int i = 123;
object o = i; // explicit boxing
![Page 5: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/5.jpg)
UNBOXING
Extracts value type from the object
Expensive computationally
5
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
![Page 6: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/6.jpg)
ISSUE FOR BOXING/UNBOXING
6
![Page 7: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/7.jpg)
ISSUE FOR BOXING/UNBOXING
7
using System;
using System.Diagnostics;
internal class Program {
private static void Main(string[] args) {
var count = 1000000000;
Console.WriteLine(“Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
object value2 = value;
}));
Console.WriteLine(“None boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = value;
}));
}
static long DoTest(int count, Action action) {
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}
0
2000
4000
6000
8000
10000
12000
1000000 10000000 100000000 1000000000
Boxing
None boxing
![Page 8: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/8.jpg)
ISSUE FOR BOXING/UNBOXING
8
using System;
using System.Diagnostics;
internal class Program {
private static void Main(string[] args) {
var count = 1000000000;
Console.WriteLine("Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = string.Format("{0}", value);
}));
Console.WriteLine("None Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = string.Format("{0}", value.ToString());
}));
}
static long DoTest(int count, Action action) {
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}
0
50000
100000
150000
200000
250000
Boxing
None Boxing
![Page 9: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/9.jpg)
ISSUE FOR BOXING/UNBOXING
9
using System;
internal class Program {
private static void Main(string[] args)
{
try {
var value1 = 1;
object value2 = value1;
var value3 = (float)value2;
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
}
![Page 10: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/10.jpg)
ISSUE FOR BOXING/UNBOXING
10
using System;
internal class Program
{
private static void Main(string[] args)
{
object a = 1;
object b = 1;
bool value = a == b;
bool value2 = a.Equals(b);
Console.WriteLine(value);
Console.WriteLine(value2);
}
}
![Page 11: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/11.jpg)
ISSUE FOR BOXING/UNBOXING
11
using System;
internal class Program
{
private static void Main(string[] args)
{
object a = 1;
object b = 1;
bool value = a == b;
bool value2 = a.Equals(b);
Console.WriteLine(value);
Console.WriteLine(value2);
}
}
![Page 12: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/12.jpg)
ISSUE FOR BOXING/UNBOXING
12
using System;
struct Counter
{
private int x;
public void Increment() { this.x++; }
public int Count { get { return this.x; } }
}
internal class Riddle
{
public readonly object counter = new Counter();
}
internal class Program
{
private static void Main(string[] args)
{
var riddle = new Riddle();
((Counter)riddle.counter).Increment();
Console.WriteLine(((Counter)riddle.counter).Count);
}
}
![Page 13: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/13.jpg)
ISSUE FOR BOXING/UNBOXING
13
using System;
struct Counter
{
private int x;
public void Increment() { this.x++; }
public int Count { get { return this.x; } }
}
internal class Riddle
{
public readonly object counter = new Counter();
}
internal class Program
{
private static void Main(string[] args)
{
var riddle = new Riddle();
((Counter)riddle.counter).Increment();
Console.WriteLine(((Counter)riddle.counter).Count);
}
}
![Page 14: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/14.jpg)
HOW TO FIND OUT
BOXING/UNBOXING
14
![Page 15: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/15.jpg)
ILDASM
15
![Page 16: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/16.jpg)
ILSPY
16
![Page 17: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/17.jpg)
.NET REFLECTOR
17
![Page 18: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/18.jpg)
BOXCOP
For .NET 3.5
18
![Page 19: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/19.jpg)
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
19
![Page 20: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/20.jpg)
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
20
![Page 21: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/21.jpg)
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
21
![Page 22: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/22.jpg)
CLR C# HEAP ALLOCATION ANALYZER
For Visual Studio 2015
22
![Page 23: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/23.jpg)
REFERENCE
Boxing and Unboxing (C# Programming Guide)
https://msdn.microsoft.com/en-
us//library/yz2be5wk.aspx?f=255&MSPPError=-
2147217396
[C#]Effective C# 條款十七:盡量減少裝箱與拆箱 -
Level Up-點部落
http://www.dotblogs.com.tw/larrynung/archive/2011/03/0
8/21712.aspx
[.NET Resource]透過BoxCop偵測程式是否存在Boxing與UnBoxing - Level Up-點部落
http://www.dotblogs.com.tw/larrynung/archive/2013/03/0
1/94727.aspx 23
![Page 24: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/24.jpg)
ReSharper - Heap Allocation Viewer Extension -
Level Up
http://larrynung.github.io/2014/08/12/resharper-heap-
allocation-viewer-extension/
Clr C# Heap Allocation Analyzer - Level Up
http://larrynung.github.io/2014/11/05/clr-c-number-heap-
allocation-analyzer/
24
![Page 25: Boxing & unboxing](https://reader033.fdocuments.net/reader033/viewer/2022042514/55a8ec161a28abbf2b8b4611/html5/thumbnails/25.jpg)
QUESTION & ANSWER
25