TheDeveloperBlog.com

Home | Contact Us

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

C# Decrement Loop Optimization

This C# performance article shows how to decrement loops as an optimization.

Decrement optimization. A loop that decrements is sometimes fastest.

We optimize loops by testing the iteration variable against zero. This works in many programming languages. We make loops faster by using decrement instead of increment.

Decrementing loop performance test

Increment down to zero: 644 ms
Increment up to max:    852 ms

Example. First, you can change loops to test against zero, which can slightly enhance performance. This also works in C++ and many other languages. The approach is called testing against 0 and iterating down (decrementing) instead of up.

Next: The code was benchmarked and the version that tests against zero was found to be faster. The difference here is minimal.

And: We use the for-loop statement, which uses an index and is more flexible than a foreach-loop.

C# program that decrements for-loop

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
	int val = 0;
	const int m = 10;
	const int max = 100000000;
	for (int x = 0; x < 10; x++)
	{
	    Stopwatch s1 = Stopwatch.StartNew();
	    for (int i = 0; i < m; i++)
	    {
		for (int a = max - 1; a >= 0; --a)
		{
		    if (val++ > 1000)
		    {
			val = 0;
		    }
		}
	    }
	    s1.Stop();
	    Stopwatch s2 = Stopwatch.StartNew();
	    for (int i = 0; i < m; i++)
	    {
		for (int a = 0; a < max; a++)
		{
		    if (val++ > 1000)
		    {
			val = 0;
		    }
		}
	    }
	    s2.Stop();
	    Console.WriteLine("{0},{1}",
		s1.ElapsedMilliseconds,
		s2.ElapsedMilliseconds);
	}
    }
}

Output

647,852
644,852
643,855
644,853
643,851
642,852
643,852
644,852
643,851
643,853

Testing against zero. The reason testing against the zero value is faster is possibly because X86 chips, including Pentium, Celeron, Core 2 Duo, and AMD Athlon, have special instructions and optimizations for this test.

Summary. We decremented loops in this interesting micro-optimization. Decrementing a variable in a loop is twice as fast. On a modern 2 GHz computer, this will save you 1 millisecond per million iterations.

Note: This means testing against zero is a millionth of millisecond faster. This is of extremely minimal value in 99% of programs.


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