TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Unboxing Test

This C# article tests unboxing and considers its ramifications.

Unboxing reduces performance on collections.

It requires a value that was boxed inside an object reference to be taken out of that container. Boxing and unboxing are known to be slow. We examine some code and perform a benchmark.

Unboxing test using ArrayList versus List

Unbox int:   1296 ms
Generic int:  165 ms [faster]

Note: 10000 * 10000 iterations. Unboxing ints had a significant performance negative.

Example. Here we see a short benchmarking program. The test performed satisfies several criteria. First, it measures time unboxing. This is important because reading values is more frequent than writing values.

Note: It adds all the numbers in an ArrayList of 10,000 items 10,000 times, and the equivalent List<int> generic.

ArrayList

Tip: You can experiment by adding some addition statements in the code to avoid JITting.

Unboxing benchmark program: C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

class Program
{
    static void Main()
    {
	// Populate lists
	for (int i = 0; i < 10000; i++)
	{
	    _arr1.Add(0);
	    _list1.Add(0);
	}
	const int m = 10000;
	Stopwatch s1 = new Stopwatch();
	s1.Start();
	for (int i = 0; i < m; i++) // Unboxing
	{
	    U1();
	}
	s1.Stop();
	Stopwatch s2 = new Stopwatch();
	s2.Start();
	for (int i = 0; i < m; i++) // No unboxing
	{
	    R1();
	}
	s2.Stop();
	Console.WriteLine("{0},{1}",
	    s1.ElapsedMilliseconds,
	    s2.ElapsedMilliseconds);
	Console.Read();
    }

    static ArrayList _arr1 = new ArrayList();
    static List<int> _list1 = new List<int>();

    static void U1()
    {
	// Unboxing
	foreach (object i in _arr1)
	{
	    int v = (int)i;
	}
    }

    static void R1()
    {
	// None
	foreach (int i in _list1)
	{
	    int v = i;
	}
    }
}

We see that unboxing caused a performance loss over the generic collections. My measurements of time unboxing were nearly ten times higher than the version without boxing. Value type instances were made faster with generics.

Note: Previous versions of this article were incorrect in stating you can unbox objects, not just values.

So: The MSIL in this article was verified to contain the "L_0015: unbox.any int32" instruction. I apologize for the error.

Advantages. Boxing typically has no advantages. There is the possibility that generics may result in longer MSIL generated. But I was unable to verify this, and the runtime gains here likely will overshadow any of that cost.

Also: There is a difference between foreach and for with ArrayList. This is an older, slower collection than List.

Rico's Performance Quiz

Summary. We examined the some performance characteristics of unboxing integral types. Generics are far faster than ArrayList on values such as int. I always prefer generics unless the code has some quirk that may benefit from ArrayList.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf