C++ 代码有效,但似乎效率很低

标签 c++

我目前正在学习 C++ 并让我的代码执行我想要的所有操作,但似乎代码效率不高,因为我基本上将输出控制台中的代码加倍,以便它显示在文本文件中。如果可以的话,您能否解释一下我做错了什么以及您建议我做什么才能使代码更有效率。 (此外,第一列需要左对齐,第二列必须在控制台和文本文件中右对齐,我相信我做对了。)

/*  Description: This program calculates and prints the monthly paycheck for an employee (Output in command prompt and .txt file).*/

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;        
char name[256];
double gross;
double fiTax;
double sTax;
double ssTax;
double mediTax;
double pPlan;
double hInsurance;
double tax;
double total;

int main() {  
    std::cout << "Please enter your name: ";
    std::cin.getline(name, 256);        
    cout << "Please enter your gross amount: ";
    cin >> gross;            
    std::cout << std::fixed;    
    std::cout << std::setprecision(2);    
    fiTax = gross * .15;
    sTax = gross * .035;
    ssTax = gross * .0575;
    mediTax = gross * .0275;
    pPlan = gross * .05;
    hInsurance = 75;

    tax = fiTax + sTax + ssTax + mediTax + pPlan + hInsurance;    
    total = gross - tax;    
    system("cls");

    ofstream file;
    file << std::fixed << std::setprecision(2);
    file.open("report.txt");    

    cout << left<< setw(28) << name << endl;
    file << left << setw(28) << name << endl;

    cout << left << setw(28) << "Gross Amount: ............ $";
    cout << right << setw(7) << gross << endl;

    file << left << setw(28) << "Gross Amount: ............ $";
    file << right << setw(7) << gross << endl;

    cout << left << setw(28) << "Federal Tax: ............. $";
    cout << right << setw(7) << fiTax << endl;

    file << left << setw(28) << "Federal Tax: ............. $";
    file << right << setw(7) << fiTax << endl;

    cout << left << setw(28) << "State Tax: ............... $";
    cout << right << setw(7) << sTax << endl;

    file << left << setw(28) << "State Tax: ............... $";
    file << right << setw(7) << sTax << endl;

    cout << left << setw(28) << "Social Security Tax: ..... $";
    cout << right << setw(7) << ssTax << endl;

    file << left << setw(28) << "Social Security Tax: ..... $";
    file << right << setw(7) << ssTax << endl;

    cout << left << setw(28) << "Medicare/medicaid Tax: ... $";
    cout << right << setw(7) << mediTax << endl;

    file << left << setw(28) << "Medicare/medicaid Tax: ... $";
    file << right << setw(7) << mediTax << endl;

    cout << left << setw(28) << "Pension Plan: ............ $";
    cout << right << setw(7) << pPlan << endl;

    file << left << setw(28) << "Pension Plan: ............ $";
    file << right << setw(7) << pPlan << endl;

    cout << left << setw(28) << "Health Insurance: ........ $";
    cout << right << setw(7) << hInsurance << endl;

    file << left << setw(28) << "Health Insurance: ........ $";
    file << right << setw(7) << hInsurance << endl;

    cout << left << setw(28) << "Net Pay: ................. $";
    cout << right << setw(7) << total << endl;

    file << left << setw(28) << "Net Pay: ................. $";
    file << right << setw(7) << total << endl;

    file.close();        
    return 0;
}

最佳答案

  1. 您对效率低下的怀疑完全没有根据且无关紧要。如此琐碎的 I/O 绑定(bind)代码完全不需要优化,如果您从头开始编写整个 I/O 子系统,任何性能提升都是微不足道且毫无意义的。

  2. 您没有目标效率指标,也没有任何效率衡量指标。换句话说,您既不知道您的代码有多快(或多慢),也不知道它应该多快。没有测量和目标,所有优化都是无用的(或者充其量是非常浪费的。)

  3. 您的代码可以看起来更好。无论多余的空行是您的工作还是将代码粘贴到此处的副作用,您都不会费心删除它们。您应该记住代码的外观很重要。 (更新:我看到你已经解决了这个问题。荣誉!)

  4. 请不要使用全局变量,除非你有一些经验并且你判断你确实需要它们。在这种情况下,你不需要。如果您将变量的作用域更改为本地,请记住也要初始化它们。

  5. 不要使用字符数组来表示字符串。使用 std::string。它们具有阵列的所有功能以及更多功能,而且更安全、更方便。

  6. 一种避免打字/复制+粘贴并消除代码冗余的方法(这非常糟糕)是使用 std::cout 的父类对象的类型和 std::ofstream 类型,即 std::ostream。您可以编写将这种类型的对象作为参数的函数,并且仅一次写出您想要写出的内容,然后您将调用这些函数两次:一次使用 std::cout ,一次使用你的文件。

抛开所有这些要点,我希望您记住这一点:不要担心性能和优化,除非您可以客观地证明这是一个问题。

(对这个回复的语气感到抱歉;OP 说他是初学者,这让我有一种说教的心情!)

更新:你可以这样写一个函数:

void LineOut (std::ostream & os, std::string const & entry, double value)
{
    int dots = 28 - 2 - int(entry.size()); // 2 for ": "
    if (dots < 0) dots = 0;
    os << entry << ": " << std::string('.', dots) << "$" << value << std::endl;
}

// Call it like this:
LineOut(std::cout, "Gross Amount", gross);
LineOut(     file, "Gross Amount", gross);

现在,您将为每一行输出调用此函数两次:一次用于 cout,一次用于您的文件。

显然还有其他更好的方法,但我怀疑对于这个小项目来说它们是否值得。

关于C++ 代码有效,但似乎效率很低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157922/

相关文章:

c++ - 将 vector<unique_ptr<Derived>> move 到 vector<unique_ptr<Base>>

c++ - 如何使用 std::greater 对 C++ 映射键进行排序?

c++ - io_service::run() 的专用线程

c++ - 为什么使用 mkdir() 函数比使用系统 ('mkdir path' 快得多)?

c++ - 基于返回类型的转换和重载扣除

c++ - 在 C++ 中通过 backgroundworker ReportProgress 发送 int 数组

c++ - 转换到一个类并从兄弟类调用函数?

c++ - 无法构建 NetBeans C/C++ 项目

C++,Qt : Variable named "slots" in included external library

c++ - 识别字节序相关的问题