调用简短方法会影响效率吗?

时间:2015-09-04 03:03:02

标签: java performance function

我在这附近创建了一些帖子。我想进入良好的编程习惯。我创建了一个BMR计算器,以下类将所有用户数据和方法打包在一起:

public class User {

    private int age;
    private String gender; // todo: use an Enum
    private double height; // height stored in cm, weight in kg (so if user enters in feet/lbs, conversions are done to cm/kg and *THEN* passed through to constructor below)
    private double weight;
    private double activityMultiplier; // todo: use an Enum  (possibly)
    private int bmr;
    private int tdee;

    // This constructor is called everytime the program is run, used to create a User object to pack all user information together. This information comes from the UI which is coded in BMRMain
    public User(int age, String gender, double height, double weight,
        double activityMultiplier) {
        this.age = age;
        this.gender = gender;
        this.height = height;
        this.weight = weight;
        this.activityMultiplier = activityMultiplier;

        bmr = calcBMR();
        tdee = calcTDEE(bmr); // Calculates and stores tdee regardless of user selecting the option, but does not display if user did not select the option
    }

    /**
     * If user input is correct, this method will calculate the BMR value of the user given their input and measurement choices.
     * 
     * @param None
     * @return BMR Value
     */
    public final int calcBMR() {
        int offset = gender.equals("M") ? 5 : -161;
        // This is the body of the calculations - different offset used depending on gender. Conversions to kg and cm done earlier so no conversions needed here.
        // The formula for male and female is similar - only the offset is different.
        return (int) (Math.round((10 * weight) + (6.25 * height) - (5 * age) + offset)); // This is the Miffin St-Jeor formula, calculations done in cm/kg
        }

    /**
     * If the user selects the TDEE option, this method will be executed after the calcBMR() method. 
     * A value from the calcBMR() method will be passed down to this method, and is multiplied
     * by the activity level parameter passed into this method.
     * 
     * @param bmr (output from calcBMR() method
     * @return TDEE Value
     */
    public final int calcTDEE(int bmr) {
        return (int) Math.round(bmr * activityMultiplier);
    }


    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public double getHeight() {
        return height;
    }

    public double getWeight() {
        return weight;
    }

    public double getActivityMultiplier() {
        return activityMultiplier;
    }

    public int getBMR() {
        return bmr;
    }

    public int getTDEE() {
        return tdee;
    }

}

在构造函数中注意始终调用calcTDEE()(也请注意注释)。我的UI为用户提供了关于他们是否不想计算TDEE的选项,但即使他们选择否,它也会计算并将其存储在变量中(但不会将值输出给用户)。

CalcTDEE()是一种简短的方法,但即使用户不想看到它,计算此值也是低效的?我输了什么吗?如果是这样,那么更好的方法是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

在这种特殊情况下,它可能无关紧要。但是,如果calcBMR的性能出现问题,请对bmr值进行延迟计算。将bmr初始化为无效值,如果getBMR无效,则将bmr初始化为bmr。返回bmr。

其他一些改进,我可以建议:

  1. 不要调用构造函数中可以重写的方法。要么把它们变成私人的,要么是最终的。
  2. 您正在使用一些幻数:5,-161等。在类中定义static final个变量。这应该使您的代码更容易理解。