C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Performance of Floating-Point Data Types
This section provides a tutorial example on how to compare performance of floating-point data types: 'float', 'double', and 'decimal'
In order to find out how much extra time an arithmetic operation will take on decimal type comparing to the float and double types, I wrote the following program:
// TimeReals.cs // Copyright (c) 2006 HerongYang.com. All Rights Reserved. using System; class TimeReals { static void Main() { long m = 100000; //long n = 225; //i, p, u long n = 100; //c, p, i float fs = 1.0f/3.0f; float ff = 2.0f/3.0f; float fi = fs; TimeSpan fd = LoopFloat(m, n, ref fs, ref ff); Console.WriteLine("Float: time = {0} ", fd); Console.WriteLine(" Input = {0} ", fi); Console.WriteLine(" Output = {0} ", fs); double ds = 1.0d/3.0d; double df = 2.0d/3.0d; double di = ds; TimeSpan dd = LoopDouble(m, n, ref ds, ref df); Console.WriteLine("Double: time = {0} ", dd); Console.WriteLine(" Input = {0} ", di); Console.WriteLine(" Output = {0} ", ds); decimal ms = 1.0m/3.0m; decimal mf = 2.0m/3.0m; decimal mi = ms; TimeSpan md = LoopDecimal(m, n, ref ms, ref mf); Console.WriteLine("Decimal: time = {0} ", md); Console.WriteLine(" Input = {0} ", mi); Console.WriteLine(" Output = {0} ", ms); Console.WriteLine("Accuracy:"); Console.WriteLine(" Float = {0} ", (fs-fi)/fi); Console.WriteLine(" Double = {0} ", (ds-di)/di); Console.WriteLine(" Decimal = {0} ", (ms-mi)/mi); Console.WriteLine("Performance:"); Console.WriteLine(" Float = {0} ", fd.TotalMilliseconds/dd.TotalMilliseconds); Console.WriteLine(" Double = {0} ", dd.TotalMilliseconds/dd.TotalMilliseconds); Console.WriteLine(" Decimal = {0} ", md.TotalMilliseconds/dd.TotalMilliseconds); } private static TimeSpan LoopFloat(long m, long n, ref float s, ref float f) { long i, j; DateTime t1 = DateTime.UtcNow; for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { s = s*f; } for (j=1; j<=n; j++) { s = s/f; } } DateTime t2 = DateTime.UtcNow; TimeSpan duration = t2.Subtract(t1); return duration; } private static TimeSpan LoopDouble(long m, long n, ref double s, ref double f) { long i, j; DateTime t1 = DateTime.UtcNow; for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { s = s*f; } for (j=1; j<=n; j++) { s = s/f; } } DateTime t2 = DateTime.UtcNow; TimeSpan duration = t2.Subtract(t1); return duration; } private static TimeSpan LoopDecimal(long m, long n, ref decimal s, ref decimal f) { long i, j; DateTime t1 = DateTime.UtcNow; for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { s = s*f; } for (j=1; j<=n; j++) { s = s/f; } } DateTime t2 = DateTime.UtcNow; TimeSpan duration = t2.Subtract(t1); return duration; } }
See next section for the output of this example and discussion.
Table of Contents
Logical Expressions and Conditional Statements
Precision of Floating-Point Data Types
Precision of Floating-Point Data Types - Test
►Performance of Floating-Point Data Types
Performance of Floating-Point Data Types - Test
IEEE 754 Standards - "float" and "double"
IEEE 754 Standards - "float" and "double" - Test
Binary Representation of "decimal"
Accuracy of "decimal" Data Type
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation