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) 2015, 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.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

Floating-Point Data Types

 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

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version