C # do a write TXT file stream test, why the low-profile machine write fast

Source: Internet
Author: User

Test machine: Notebook i7 8G Solid State Drive

Because of the read code to write the TXT mode, read the code frequency is very high, the file name is the date format, the current use of each reading code to open the file to write the way, why no use SB, because of fear of loss of data lost. So take each deposit in the same way.

Today you want to optimize your code to take a file stream open without having to test the efficiency every time you turn it off.

1: New Project Writetxt

2: Add clock event, write txt at timed time.

3: Add two Write methods

4: Time separately. View efficiency.

   Static Objectobj =New Object();  Public Static voidWritesvnlog (stringresult) {            Lock(obj) {Stopwatch SW1=NewStopwatch (); SW1.                Start (); stringTime =DateTime.Now.ToLongTimeString (); stringPath = Application.startuppath +"\\upload\\"; if(!directory.exists (path))                Directory.CreateDirectory (path); Path= Path +"\\LOGSVN"+ DateTime.Now.ToString ("YYYYMMDD") +". txt"; if(!file.exists (path)) File.create (Path).                Close (); //FileStream FileStream = new FileStream (path, System.IO.FileMode.Append, System.IO.FileAccess.Write); //System.IO.StreamWriter sw = new System.IO.StreamWriter (FileStream, encoding.default);FileStream fs =NewFileStream (Path, filemode.append, FileAccess.Write); StreamWriter SW=NewStreamWriter (FS, Encoding.default); Sw. WriteLine (Result+"  "+Time ); Sw.                Close (); Sw.                Dispose (); Fs.                Close (); Fs.                Dispose (); SW1.                Stop (); Debug.WriteLine ("spents MS:"+SW1.            Elapsed.totalmilliseconds); }        }
Results:
Spents MS:2.4102 when ms:2.8373 time ms:2.987 spents ms:2.8673 Spents MS:3.0787 when MS:3.4216 spents ms:2.558 Time: 2.6216 MS:2.5094 spents ms:2.4726

Open File Size 216k

Test 2: Open 30M file test time:

Spents ms:11.0885
Spents ms:2.5825
Spents ms:2.5461
Spents ms:2.6026
Spents ms:6.1945
Spents ms:2.5809
Spents ms:3.247
Spents ms:5.4372
Spents ms:2.4047
Spents ms:2.5754
Spents ms:2.5532
Spents ms:2.4252
Spents ms:3.2643
Spents ms:2.3798
Spents ms:2.5797
Spents ms:2.6018
Spents ms:3.1233
Spents ms:5.5288
Spents ms:2.9044
Spents ms:2.0491
Spents ms:2.7034
Spents ms:5.2598
Spents ms:2.2486

A large value appears in the middle. The computer is not saturated at this time, the overall value changes little.

Continue to observe, there are fluctuations.

Change 20ms

Spents ms:2.3521
Spents ms:3.0119
Spents ms:2.5323
Spents ms:3.2632
Spents ms:2.5801
Spents ms:2.4971
Spents ms:2.7784
Spents ms:3.0281
Spents ms:2.351
Spents ms:2.6322
Spents ms:3.0763
Spents ms:2.1234
Spents ms:2.569
Spents ms:2.4023
Spents ms:2.6595

Test three, change the program, change Intel Dual Core 3.0 4G memory, mechanical hard disk PC test old machine

Because the remote machine does not have a VS environment, the time output to txt

1: First Test in native

2: Remote machine:

Alas, the configuration of low-speed machine unexpectedly write fast, notebook or solid-state drive, is WIN10 SSD write slow?

C # do a write TXT file stream test, why the low-profile machine write fast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.