C# Assign Locals

C# programming language

A variable stores the result of an expression. This is called assignment. The concept of assignment in programming languages introduces the complexity of time. It increases the cost of certain methods.

Benchmark results

Use expression:      19.52 ns
Use local variables: 22.32 ns

Example

Programming tip

Instead of assigning local variables to the results of computations such as the results of method invocations, you can put the method invocations directly into the final expression. This reduces intermediate memory locations.

Next:A method that uses assignment to store the results of internal method calls can be rewritten as an expression-based method.

Arithmetic Optimization
Program that tests assignment and variables: C#

using System;

class Program
{
    static int _field = 5;

    static int Value1()
    {
	// This method just returns an integer.
	return _field * 2;
    }

    static int Value2()
    {
	// This method also returns an integer.
	return _field * 3;
    }

    static int UseExpression()
    {
	// This method uses an expression-based statement.
	// ... No temporary variables are needed here.
	return Value1() +
	    Value2() +
	    Value1() +
	    Value2();
    }

    static int UseLocals()
    {
	// This method uses four variables to store intermediate values.
	// ... These require storage locations.
	int value1 = Value1();
	int value2 = Value2();
	int value3 = Value1();
	int value4 = Value2();
	return value1 + value2 + value3 + value4;
    }

    static void Main()
    {
	// Test the methods.
	Console.WriteLine(UseExpression());
	Console.WriteLine(UseLocals());
    }
}

Output

50
50
Squares

In this example, UseExpression and UseLocals both perform the same computation. But the UseExpression method places the result of the Value1 and Value2 methods onto the evaluation stack for processing.

And:UseLocals places the results into local variable slots and then into the evaluation stack. It uses several local variable slots.

UseLocals generates more IL opcodes to perform its computation. It uses several stloc instructions to store the values into local variable slots. It then uses ldloc instructions to read the values from those local variable slots.

ldloc InstructionIntermediate Language

UseExpression simply places the result of the internal method calls onto the evaluation stack, and then adds the values from those locations. To verify on your computer, please use the IL Disassembler tool.

IL Disassembler

Benchmark

Performance optimization

The exact methods specified in the first program, UseExpression and UseLocals, were tested. In the benchmark harness, a loop tested the UseExpression ten times over 100 million iterations.

Then:A loop tested the UseLocals method in the same way. The average time of ten statements was computed.

Benchmark

Result:The UseExpression method took between and 2-3 nanoseconds fewer for a group of ten calls.

Concept

Structure and Interpretation of Computer Programs

Let us explore the concept of assignment and expression-based programming. This idea is described in the classic programming textbook Structure and Interpretation of Computer Programs.

This book delves into how the ability to assign variables to memory addresses in programs greatly complicates the computational model. It makes program interpretation and compilation more complex.

SICP

So:Assignment introduces the complexity of time into programs. A program can only be understood at a certain point.

And:The complexity of time drives the compiler's inability to optimize the assignment method.

Spacetime

Summary

Framework: NET

We explored the concept of assignment and expressions in the C# language. We inspected the C# language's implementation for the concept of assignment. This uses local variable slots and the evaluation stack.

Also:We benchmarked the methods shown. We found that local variables can increase the cost of a method.


C#: Method