简体   繁体   English

使用集合属性了解时间复杂度

[英]Understanding Time Complexity with Set Properties

I am attempting to reduce the lines in my code as a means to improve the execution speed of my windows application.我正在尝试减少代码中的行数,以提高 windows 应用程序的执行速度。 So far, I've come to understand the usefulness of using properties when it comes to adding conditions within the get and set fields.到目前为止,我已经了解了在 get 和 set 字段中添加条件时使用属性的用处。 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.如果有人可以就当前的改进是否有助于减少处理时间以及它是否遵循最简单的 Big-O 表示法(希望为 O(n))分享一些建议,我将不胜感激。

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;
}

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...决定速度的因素首先是方法调用的数量(属性是对 getter 和 setter 的调用),以及类型转换、数据类型(例如数组或 List<>)、循环(for vs foreach)和条件测试、计算,以及诸如堆和堆栈使用、memory 访问、I/O 访问、硬件和软件中断之类的事情……

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...为了提高速度,您应该:减少方法调用,消除无用代码,小心 select 类型,使用 for instead 或 foreach,避免 Linq,使用 StringBuilder 而不是 String 来操作大字符串...

Your "old" code (5 lines) is nearly optimized as-is.您的“旧”代码(5 行)几乎按原样优化。

Your "new" code (10 lines) is not because of the call introduced with the get and set even if the compiler optimize them.即使编译器对其进行了优化,您的“新”代码(10 行)也不是因为getset引入的调用。

But you can do better using a boolean:但是您可以使用 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.您当然可以使用 IL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM