C # file I / O efficiency

I did my homework, here is a description of the problem:

Your program should work as follows:

  • Ask the user to provide a file name. Get the file name and save it.
  • Open the file.
  • Read the temperature and wind speed from the file. Both values ​​must be stored in variables declared as double. The file is a text file. Each line of the file contains the value of temperature and wind speed.
  • Calculate the wind cooling coefficient using a method written by a programmer and show the result in the form:

    At t = temperature from the file and v = wind speed from the file, Wind cold index = calculated result of Fahrenheit.

    Show all numbers with two digits after the decimal point. (Remember - there are no magic numbers!)

  • Repeat these steps until the end of the file is detected.

I completed the task, my code is below, I'm just wondering if there is a way to make it more efficient, or if there are several different and creative ways to solve this problem, I already turned it on and got 50/50, but I'm just wondering how some of you are advanced, and experienced programmers approach this problem.

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 

Feel free to tell me what you think about it.

+7
performance c # file-io
source share
9 answers

You will not get much more than for an IO file in C #. Depending on the size of the dataset, it might be worth using a buffered reader, but for fairly small files this is simply not worth it. I would leave it as it is.

+7
source share

Your method looks good, but:

  • It would be better if you used PascalCase for constants, like the fact that you use conventions for C #.
  • you must wrap the StreamReader in a using statement so that it fits correctly after completion.
  • You should probably also include it in a try block (and catch to handle the exception correctly) to make sure you are not getting a FileNotFound exception.
  • It might be better to create a while loop structure as follows:

while((line = windChillDoc.ReadLine()) != null) { ... } [Darn formatting will not work correctly!]

Other than that, I would not know, because I am not familiar with weather calculations :)

+9
source share

Most of your comments are outsiders. The code should tell you how ... comments should tell you why.

+6
source share

Slight nitpick, but "WindChillCalc" should be "CalcWindChill" if you use English method names (first comes the verb).

+6
source share
 string filePath = ""; ... filePath = Console.ReadLine(); 

Do not initialize values ​​that are never used; and keep the declaration and initialization close to each other:

 string filePath = Console.ReadLine(); 

using , but do not use @ unnecessarily:

 new StreamReader(@filePath); 

should be simple:

 new StreamReader(filePath); 

Personally, I use LINQ for a linear reader, but that's just me; -p

+5
source share

Why do / while? In your case, you are checking for null. While you are checking for null. Why not just make that expression?

 string line; while((line = windChillDoc.ReadLine()) != null) { //Logic } 

EDIT . Fixed compilation error. It's funny that I had it originally. This Rich Text Box needs a compiler !: P

+4
source share

Although this does not apply to performance (main question)

IMO:

 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; 

not much better than a magic number. Looking at this, I have no idea that FIRST_EQUATION_NUMBER used differently than in any equation, and I can’t say that they are in the same equation or do you have four equations that use different numbers? They can also be placed in the actual method, as this is the only place they are used.

I would change degreeSymbol to const, and not to its work from const int later.

 const char DEGREE_SYMBOL = (char)176; 
+3
source share

If you get style markings, etc., then there are a couple of extremely minor things

  • Initialization of doubles to 0.0 is redundant.
  • string.Empty is preferable to ""
  • Your windchill method can be changed simply (although at compile time I think wci will be optimized - so it is functionally the same):

(changing the formatting for reading SO)

 static double WindChillCalc(double temperature, double ws) { return FIRST_EQUATION_NUMBER + (SECOND_EQUATION_NUMBER * temperature) - (THIRD_EQUATION_NUMBER * (Math.Pow(ws, EQUATION_EXPONENT))) + (FOURTH_EQUATION_NUMBER * temperature * (Math.Pow(ws, EQUATION_EXPONENT))); } 
+2
source share

In small academic programs like this, if you aren’t doing something really dumb, performance will not be a problem. An easy way to tell if performance is a problem is to ask, "Does it make me wait?"

If there was a huge amount of input, I would ask who provides the input and who reads the result. This will tell me if I can do I / O in binary, and not in text format, because, as before, the main part of the processing will be to convert the text to numbers in the input, and numbers to text in the output, especially floating comma.

0
source share

All Articles