SlideShare a Scribd company logo
BOXING & UNBOXING
Larry Nung
AGENDA
Boxing/UnBoxing
Issue for boxing/unboxing
How to find out boxing/Unboxing
2
BOXING/UNBOXING
3
BOXING
 Value Type => Object
 New object must be allocated and constructed
 Expensive computationally
4
int i = 123;
object o = i; // explicit boxing
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
ISSUE FOR BOXING/UNBOXING
6
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
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
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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
HOW TO FIND OUT
BOXING/UNBOXING
14
ILDASM
15
ILSPY
16
.NET REFLECTOR
17
BOXCOP
 For .NET 3.5
18
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
19
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
20
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
21
CLR C# HEAP ALLOCATION ANALYZER
 For Visual Studio 2015
22
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
 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
QUESTION & ANSWER
25

More Related Content

What's hot

Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
Docent Education
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Storage class
Storage classStorage class
Storage class
Joy Forerver
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 

What's hot (20)

Wrapper class
Wrapper classWrapper class
Wrapper class
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
class and objects
class and objectsclass and objects
class and objects
 
Storage class
Storage classStorage class
Storage class
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Difference between association, aggregation and composition
Difference between association, aggregation and compositionDifference between association, aggregation and composition
Difference between association, aggregation and composition
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java I/O
Java I/OJava I/O
Java I/O
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 

Viewers also liked

Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
chathuranga kasun bamunusingha
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
SAMIR BHOGAYTA
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5
Bhushan Mulmule
 
Digital signatures and e-Commerce
Digital signatures and e-CommerceDigital signatures and e-Commerce
Digital signatures and e-Commerce
Naveen Jakhar, I.T.S
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
Arun Prasad
 

Viewers also liked (7)

Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5
 
Digital signatures and e-Commerce
Digital signatures and e-CommerceDigital signatures and e-Commerce
Digital signatures and e-Commerce
 
C#.NET
C#.NETC#.NET
C#.NET
 
C# basics
 C# basics C# basics
C# basics
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 

Similar to Boxing & unboxing

classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
gourav kottawar
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
SaadAsim11
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
gourav kottawar
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
sktambifortune
 
Virtual function
Virtual functionVirtual function
Virtual function
harman kaur
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
SantoshVarshney3
 
662305 09
662305 09662305 09
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
Sajid Alee Mosavi
 
C++.pptx
C++.pptxC++.pptx
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
Eelco Visser
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
nsm.nikhil
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
Karuppaiyaa123
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 
CS Project-Source code for shopping inventory for CBSE 12th
CS Project-Source code for shopping inventory for CBSE 12thCS Project-Source code for shopping inventory for CBSE 12th
CS Project-Source code for shopping inventory for CBSE 12th
Sudhindra Mudhol
 
wk2utility classes.zipApplicationUtilities.csusing System.docx
wk2utility classes.zipApplicationUtilities.csusing System.docxwk2utility classes.zipApplicationUtilities.csusing System.docx
wk2utility classes.zipApplicationUtilities.csusing System.docx
ericbrooks84875
 

Similar to Boxing & unboxing (20)

classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Class 6 2ciclo
Class 6 2cicloClass 6 2ciclo
Class 6 2ciclo
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
662305 09
662305 09662305 09
662305 09
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
CS Project-Source code for shopping inventory for CBSE 12th
CS Project-Source code for shopping inventory for CBSE 12thCS Project-Source code for shopping inventory for CBSE 12th
CS Project-Source code for shopping inventory for CBSE 12th
 
wk2utility classes.zipApplicationUtilities.csusing System.docx
wk2utility classes.zipApplicationUtilities.csusing System.docxwk2utility classes.zipApplicationUtilities.csusing System.docx
wk2utility classes.zipApplicationUtilities.csusing System.docx
 

More from Larry Nung

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
Larry Nung
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
Larry Nung
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
Larry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
Larry Nung
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
Larry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
Larry Nung
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
Larry Nung
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
Larry Nung
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
Larry Nung
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
Larry Nung
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
Larry Nung
 
Web deploy
Web deployWeb deploy
Web deploy
Larry Nung
 
SikuliX
SikuliXSikuliX
SikuliX
Larry Nung
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
Larry Nung
 
Common.logging
Common.loggingCommon.logging
Common.logging
Larry Nung
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
Larry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5
Larry Nung
 
Regular expression
Regular expressionRegular expression
Regular expression
Larry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
Larry Nung
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
Larry Nung
 

More from Larry Nung (20)

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
 
Web deploy
Web deployWeb deploy
Web deploy
 
SikuliX
SikuliXSikuliX
SikuliX
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
 
Common.logging
Common.loggingCommon.logging
Common.logging
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5
 
Regular expression
Regular expressionRegular expression
Regular expression
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
 

Recently uploaded

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 

Recently uploaded (20)

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 

Boxing & unboxing

  • 4. BOXING  Value Type => Object  New object must be allocated and constructed  Expensive computationally 4 int i = 123; object o = i; // explicit boxing
  • 5. 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
  • 7. 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
  • 8. 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
  • 9. 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); } } }
  • 10. 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); } }
  • 11. 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); } }
  • 12. 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); } }
  • 13. 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); } }
  • 14. HOW TO FIND OUT BOXING/UNBOXING 14
  • 19. RESHARPER - HEAP ALLOCATION VIEWER EXTENSION 19
  • 20. RESHARPER - HEAP ALLOCATION VIEWER EXTENSION 20
  • 21. RESHARPER - HEAP ALLOCATION VIEWER EXTENSION 21
  • 22. CLR C# HEAP ALLOCATION ANALYZER  For Visual Studio 2015 22
  • 23. 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
  • 24.  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