此代码如何提高速度和代码? [英] How does this code improve the speed and the code?

查看:59
本文介绍了此代码如何提高速度和代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StreamWriter sw1 = new StreamWriter(".\\wj\\" + "UD.txt");
            StreamWriter sw2 = new StreamWriter(".\wj\\" + "EW.txt");
            StreamWriter sw3 = new StreamWriter(".\\wj件\\" + "NS.txt");
            using (StreamReader sr = new StreamReader(txt1.Text, System.Text.Encoding.UTF8, false))
            {
                char[] c1 = null;
                //This is an arbitrary size for this example.
                while (sr.Peek() >= 0)
                {
                    c1 = new char[24];
                  int jj=  sr.Read(c1, 0, c1.Length);
                    if (jj == 24)
                    {
                        for (int b = 0; b < c1.Length / 8; b++)
                        {

                            string s1 = new string(c1.Take(8).ToArray());
                            string s2 = new string(c1.Skip(8).Take(8).ToArray());
                            string s3 = new string(c1.Skip(16).Take(8).ToArray());
                            sw1.WriteLine(Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber));
                            sw2.WriteLine(Int32.Parse(s2, System.Globalization.NumberStyles.HexNumber));
                            sw3.WriteLine(Int32.Parse(s3, System.Globalization.NumberStyles.HexNumber));
                        }
                    }
                    //The output will look odd, because
                    //only five characters are read at a time.

                }
                sw1.Close();
                sw2.Close();
                sw3.Close();
            }

txt1.txt,其中有很多数据文件,而且全部都在同一行


请验证我的帐户

txt1.txt,There's a lot of data in the file, and it's all in one line


please verify my account

推荐答案

Hi lctk,

Hi lctk,

谢谢您在这里发布.

根据您的代码,我创建了一些演示来测试速度,您可以参考以下代码(因为我不知道 您的数据大小.因此,我制作了一个数据其中包含2000个字符):

Base on your code,I create some demo to test the speed, you could take reference with the following code( Because I don't know the size of your data.So I make a data  that include 2000 char ):

 

1.展开数组的长度(花费9毫秒):

c1 = new char[240];
                    int jj = sr.Read(c1, 0, c1.Length);
                    if (jj >= 24)
                    {
                        for (int b = 0; b < (jj / 8 )-2; b++)
                        {

                            string s1 = new string(c1.Take(8).ToArray());
                            string s2 = new string(c1.Skip(8).Take(8).ToArray());
                            string s3 = new string(c1.Skip(16).Take(8).ToArray());
                            sw1.WriteLine(Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber));
                            sw2.WriteLine(Int32.Parse(s2, System.Globalization.NumberStyles.HexNumber));
                            sw3.WriteLine(Int32.Parse(s3, System.Globalization.NumberStyles.HexNumber));
                        }
                    }

该代码适用于大数据,您可以对其进行调整数组长度以适合数据量.

2.使用File.ReadAllText代替StreamReader 支出 12毫秒

2.Using the File.ReadAllText instead of StreamReader (spend 12 millisecond).

 string sr = File.ReadAllText(@".\\txt1.Text", System.Text.Encoding.UTF8);
            char[] c1 = sr.ToArray();

            for (int b = 0; b < (c1.Length / 8)-2; b += 3)
            {

                String s1 = new String(c1.Skip(b * 8).Take(8).ToArray());
                string s2 = new string(c1.Skip((b + 1) * 8).Take(8).ToArray());
                string s3 = new string(c1.Skip((b + 2) * 8).Take(8).ToArray());

                sw1.WriteLine(Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber));
                sw2.WriteLine(Int32.Parse(s2, System.Globalization.NumberStyles.HexNumber));
                sw3.WriteLine(Int32.Parse(s3, System.Globalization.NumberStyles.HexNumber));
}

3.File.ReadAllTex方法适用于少量数据,这对于提高处理少量数据的运行速度非常有利 花费13毫秒 ).

3.The method File.ReadAllTex is suitable for a small amount of data,it is well good to improve the speed of running in handing small amount of data (spend 13 millisecond).

string[] sr = File.ReadAllLines(@".\\txt1.Text", System.Text.Encoding.UTF8);
            char[] c1 = sr[0].ToArray();


            for (int b = 0; b < (c1.Length / 8) - 2; b += 3)
            {

                String s1 = new String(c1.Skip(b * 8).Take(8).ToArray());
                string s2 = new string(c1.Skip((b + 1) * 8).Take(8).ToArray());
                string s3 = new string(c1.Skip((b + 2) * 8).Take(8).ToArray());

                sw1.WriteLine(Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber));
                sw2.WriteLine(Int32.Parse(s2, System.Globalization.NumberStyles.HexNumber));
                sw3.WriteLine(Int32.Parse(s3, System.Globalization.NumberStyles.HexNumber));
            }

我对该演示进行了测试,但速度并没有明显提高.您可以使用该方法在您的项目上进行测试.

我建议您可以选择第一种方法.我测试了代码,它运行良好.

我希望这会对您有所帮助.

最好的问候,

Wendy


这篇关于此代码如何提高速度和代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆