C#文件I/O效率

Ale*_*lex 7 c# performance file-io

我做了一个家庭作业,这里是问题陈述:

您的程序应该如下工作:

  1. 要求用户提供文件名.获取文件名并保存.
  2. 打开文件.
  3. 从文件中读取温度和风速.这两个值都应存储在声明为double的变量中.该文件是文本文件.文件的每一行包含温度和风速值.
  4. 使用程序员编写的方法计算风寒因子,并以下列形式显示结果:

    对于t =来自file的温度和v =来自文件的风速Wind chill index =计算结果华氏度.

    显示小数点后两位数的所有数字.(记住 - 没有神奇的数字!)

  5. 重复这些步骤,直到遇到文件结尾.

我已经完成了作业,我的代码在下面,我只是想知道是否有任何方法可以提高效率,或者如果有一些不同的创造性方法来解决这个问题,我已经把它转到了50/50 ,但我很好奇你们有些高级和熟练的程序员会如何解决这个问题.

using System;
using System.IO;

class Program
{
    // declare constants to use in wind chill factor equation - no magic numbers
    const double FIRST_EQUATION_NUMBER = 35.74;
    const double SECOND_EQUATION_NUMBER = 0.6215;
    const double THIRD_EQUATION_NUMBER = 35.75;
    const double FOURTH_EQUATION_NUMBER = 0.4275;
    const double EQUATION_EXPONENT = 0.16;
    const int DEGREE_SYMBOL_NUMBER = 176;

    static void Main()
    {
        // declare and initialize some variables
        string filePath = "";
        string line = "";
        double temperature = 0.0;
        double windSpeed = 0.0;
        double windChillFactor = 0.0;
        char degreeSymbol = (char)DEGREE_SYMBOL_NUMBER;

        // ask user for a file path
        Console.Write("Please enter a valid file path: ");
        filePath = Console.ReadLine();

        // create a new instance of the StreamReader class
        StreamReader windChillDoc = new StreamReader(@filePath);

        // start the read loop
        do 
        {
            // read in a line and save it as a string variable
            line = windChillDoc.ReadLine();

            // is resulting string empty? If not, continue execution
            if (line != null)
            {
                string[] values = line.Split();
                temperature = double.Parse(values[0]);
                windSpeed = double.Parse(values[1]);

                windChillFactor = WindChillCalc(temperature, windSpeed);

                Console.WriteLine("\nFor a temperature {0:f2} F{1}", temperature, degreeSymbol);
                Console.WriteLine("and a wind velocity {0:f2}mph", windSpeed);
                Console.WriteLine("The wind chill factor = {0:f2}{1}\n", windChillFactor, degreeSymbol);
            }
        } while (line != null);

        windChillDoc.Close();

        Console.WriteLine("\nReached the end of the file, press enter to exit this program");

        Console.ReadLine();
    }//End Main()

    /// <summary>
    /// The WindChillCalc Method
    /// Evaluates a wind chill factor at a given temperature and windspeed
    /// </summary>
    /// <param name="temperature">A given temperature</param>
    /// <param name="ws">A given windspeed</param>
    /// <returns>The calculated wind chill factor, as a double</returns>
    static double WindChillCalc(double temperature, double ws)
    {
        double wci = 0.0;
        wci = FIRST_EQUATION_NUMBER + (SECOND_EQUATION_NUMBER * temperature) - (THIRD_EQUATION_NUMBER * (Math.Pow(ws, EQUATION_EXPONENT))) + (FOURTH_EQUATION_NUMBER * temperature * (Math.Pow(ws, EQUATION_EXPONENT)));
        return wci;
    }
}//End class Program 
Run Code Online (Sandbox Code Playgroud)

随意告诉我你的想法.

RCI*_*CIX 9

你的方式很好,但是:

  • 如果你使用PascalCase作为常量会更好看,因为这是c#使用的编码约定.
  • 你应该将StreamReader包装在using语句中,这样一旦你完成就可以正确处理它.
  • 您可能还应该将其包装在try块中(以及正确处理异常的catch)以确保您没有获得FileNotFound异常.
  • 以下列方式构建while循环可能是个更好的主意:

while((line = windChillDoc.ReadLine()) != null) { ... } [Darn格式化无法正常工作!]

除此之外,我不知道,因为我不熟悉天气计算:)

  • USING建议+1.http://msdn.microsoft.com/en-us/library/yh598w02.aspx (2认同)
  • 我不同意它会看起来"更好",但你是对的 - 约定规定PascalCase应该用于常量.我有点像UPPERCASE_WITH_UNDERSCORES风格,因为它清楚地区分了评论并且已经使用了这么长时间. (2认同)

Ken*_*son 7

你不会比C#中的文件IO获得更多的zippier.根据数据集的大小,使用缓冲读取器可能是值得的,但对于足够小的文件,它是不值得的.我会原样离开.


Mik*_*las 6

你的大部分评论都是无关紧要的.代码应告诉您如何......评论应告诉您原因.

  • 在像这样的家庭作业中,这是怎么回事.但总的来说你是对的. (3认同)
  • 当然,你是对的,但根据我的经验,很多大学都需要不必要的评论. (2认同)

Mat*_*att 6

轻微的挑剔,但如果您使用英语方法名称(动词先行),"WindChillCalc"应为"CalcWindChill".


Mar*_*ell 5

string filePath = "";
...
filePath = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

不要使用从未使用过的值进行初始化; 并保持声明和初始化紧密相连:

string filePath = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

using已被提及 - 但不要@不必要地使用:

new StreamReader(@filePath);
Run Code Online (Sandbox Code Playgroud)

应该只是:

new StreamReader(filePath);
Run Code Online (Sandbox Code Playgroud)

就个人而言,我会使用LINQ作为读卡器,但那只是我; -p