-1

I am attempting to reduce the lines in my code as a means to improve the execution speed of my windows application. So far, I've come to understand the usefulness of using properties when it comes to adding conditions within the get and set fields. I am not certain though if relying on properties helps improve the time complexity in comparison to setting a basic value followed by conditional statements in certain methods. I will provide an example on what I did first and what I improved. If anyone can share some advice on if the current improvement helps reduce processing time as well as if it follows the simplest Big-O Notation that is hopefully O(n), I'd appreciate the feedback.

Old

public float tempP1 = 1.0f;
public void addToP1() {
        tempP1 += 0.4f;
        tempP1 = (tempP1 > 2.0f) ? 2.0f : tempP1;
}

New

private float _tempP1 = 1.0f;
public float tempP1 { get { return this._tempP1; }
        set {
                value = (value > 2.0f) ? 2.0f : value;
                this._tempP1 = value;
        }
}

public void addToP1() {
        tempP1 += 0.4f;
}
  • 1
    I don't think anyone will be able to tell you with certainty which is faster for your use-case. Your best bet is to test it yourself in your code. Also be careful about over-engineering stuff like this. It's possible that you're spending too much time to shave a thousandth of second off your execution time. – JDB Oct 01 '19 at 16:10
  • https://ericlippert.com/2012/12/17/performance-rant/ – Peter Duniho Oct 01 '19 at 21:19

1 Answers1

1

Reducing the number of lines of code does not always imply the improvement of the execution speed.

What determines the speed is first the number of method calls (the properties are a calling to getters and setters), as well as typecast, datatypes (arrays or List<> for example), loops (for vs foreach) and condition tests, calculations, and things such as heap and stack usage, memory access, I/O access, hardware and software interrupts...

To improve speed you should : reduce methods calls, eliminates useless code, carefully select types, use for instead or foreach, avoid Linq, use StringBuilder instead of String to manipulate big strings...

Your "old" code (5 lines) is nearly optimized as-is.

Your "new" code (10 lines) is not because of the call introduced with the get and set even if the compiler optimize them.

But you can do better using a boolean:

bool reached = false;
public float tempP1 = 1.0f;
public void addToP1()
{
  if ( reached ) return;
  tempP1 += 0.4f;
  reached = tempP1 > 2.0f;
  if ( reached )
    tempP1 = 2.0f;
}

You can compile in release mode and check that optimization is enabled in the project settings/build.

Someone could probably find better optimization but it would be using esoteric code and this is generally not recommended.

You can of course use IL.

  • I was afraid that would be the case. I did notice the set property was repeatedly calling itself whenever the value was altered, adding to the lines of code. If this is the case though, then what is the main purpose of relying on properties over fields if the former is going to obviously slow the process by generating more calls? – Jesse Babineau Oct 01 '19 at 18:39
  • Properties are made to control values using getter and setter. They are very important in OOP. You may take a look at [Using Properties (C# Programming Guide)](https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/using-properties) and [Why use getters and setters/accessors?](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) and [Mutator method](https://en.wikipedia.org/wiki/Mutator_method) –  Oct 01 '19 at 18:49