简体   繁体   English

在保持良好的面向对象设计的同时,重用代码以获得性能的最佳方法是什么?

[英]What's the best way to reuse code for performance while keeping a good Object Oriented design?

I am developing a scientific program where there are several time consuming functions that are called from different other functions. 我正在开发一个科学计划,其中有几个耗时的功能,从不同的其他功能调用。 I want to optimize these calls by avoid calling the same function with the same parameters more than once without breaking OO concepts such as encapsulation. 我希望通过避免在不破坏OO概念(如封装)的情况下多次使用相同参数调用相同的函数来优化这些调用。

I tried to formulate a basic example of what I have. 我试图制定一个基本的例子。 Class A has an object of class B from where I calculate intermediate results needed by class A functions. A类有一个B类对象,我从中计算出A类函数所需的中间结果。 For example, in order for the functionA and functionB to calculate its results, each function needs to call the time_consuming_function on the object from B. Usually this functions are called from within a loop so the parameters (1 and 2) are the same among calls to functionA and functionB. 例如,为了让functionA和functionB计算其结果,每个函数都需要从B调用对象的time_consuming_function。通常这个函数是从循环内调用的,所以参数(1和2)在调用中是相同的功能A和功能B.

class ClassA {
  ClassB obj // member object

  return_type functionA(parameter1, parameter2) {
    value1 = obj.time_consuming_function(parameter1)
    value2 = obj.time_consuming_function(parameter2)

    return some_operation_with(value1, value2)
  }

  return_type functionB(parameter1, parameter2) {
    value1 = obj.time_consuming_function(parameter1)
    value2 = obj.time_consuming_function(parameter2)

    return some_operation_with(value1, value2)
  }
}

One solution would be create a structure to hold the calculated values among calls to ClassA functions, such as this example: 一种解决方案是创建一个结构来保存对ClassA函数的调用中的计算值,例如:

class ClassA {
  ClassB obj // member object

  return_type functionA(data) { // data is a structure to hold the values
    if (data.is_empty()) {
      data.value1 = obj.time_consuming_function(parameter1)
      data.value2 = obj.time_consuming_function(parameter2)
    }

    return some_operation_with(data.value1, data.value2)
  }

  return_type functionB(data) { // same data obj as before

    // this time data is not empty, so no calculation needed
    if (data.is_empty()) {
      data.value1 = obj.time_consuming_function(parameter1)
      data.value2 = obj.time_consuming_function(parameter2)
    }

    return some_operation_with(data.value1, data.value2)
  }
}

Another solution would be create a class Data to hold the data beforehand. 另一种解决方案是创建一个类Data来预先保存数据。 In this case, having the parameters 1 and 2, I would update all properties that change with this parameter on class Data and when executing my functions from class AI would only need to use the values calculated previously. 在这种情况下,使用参数1和2,我将在类Data上更新使用此参数更改的所有属性,并且当从类AI执行我的函数时,只需要使用先前计算的值。 The class A would provide an interface to the Data class. A类将提供Data类的接口。

class Data {
  ClassA class_a_obj

  void update_values_that_depend_on_parameter1(class_a_obj, parameter1) {
    value1 = class_a_obj.time_consuming_function(parameter1)
  }

  void update_values_that_depend_on_parameter2(parameter2) {
    value2 = class_a_obj.time_consuming_function(parameter2)
  }
}

class ClassA {
  ClassB obj // member object

  return_type functionA(data) {
    return some_operation_with(data.value1, data.value2)
  }

  return_type functionB(data) { // same data obj as before

    return some_operation_with(data.value1, data.value2)
  }

  return_type time_consuming_function(parameter) {
    return obj.time_consuming_function(parameter)
  }
}

I wanted to know what you think of this problem and the proposed solutions. 我想知道您对此问题的看法以及建议的解决方案。 Are there other suitable and efficient solutions? 还有其他合适而有效的解决方案吗?

TL;DR: TL; DR:

I have a program that makes several calculations and each of this calculations calls functions to get intermediate values. 我有一个程序进行多次计算,每个计算都调用函数来获取中间值。 These values are the same when called from functions that receive the same parameters, I am looking for a way to optimize these calls reusing those intermediate values. 从接收相同参数的函数调用时,这些值是相同的,我正在寻找一种方法来优化这些调用重用这些中间值。

What is the best way to reuse those values while still keeping a good Object Oriented design?? 在保持良好的面向对象设计的同时重用这些值的最佳方法是什么?

通过代理缓存对这些方法/对象的访问,缓存最近的结果并返回缓存的结果。

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

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