效能分析:重定向输入
原来不太理解输入重定向的意思,问了度娘之后了解了输入重定向是指不使用系统提供的标准输入端口,而进行重新的指定。即:"输入重定向运算符(<)"可以处理从除键盘外的设备到Windows Vista命令的输入。输入重定向总是用于发送文本文件内容到Windows Vista命令。查找了c#如何实现输入重定向,发现了博客园里的这么一篇文章对这次任务的实现有帮助: http://www.cnblogs.com/tdskee/p/5852723.html
其中讲到当 Process 将文本写入其标准流中时,通常将在控制台上显示该文本。通过重定向 StandardOutput 流,可以操作或取消进程的输出。里面的语句是:process.StartInfo.RedirectStandardInput = true; // 重定向输入流 。考虑到原来的代码在实现功能二方面是没有问题的,重定向应该是在前面加一下规定重定向输入的代码,即:if(是重定向输入) {执行原来的代码;}
所以我打算在原有代码上进行修改。查询了ProcessStartInfo 类 https://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.startinfo.aspx
我的代码修改结果如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.ComponentModel;
namespace wf
{
class Program
{
static void Main(string[] args)
{
if (args.Equals("-s"))
{
if (Program.StartInfo.RedirectStandardInput == true)
{
StreamReader ioInput = null;
ioInput = new StreamReader(Console.OpenStandardInput());
string textFileName;
Console.Write(">type ");
textFileName = Console.ReadLine();
StreamReader sreader = new StreamReader(@"C:\Users\dell-pc\desktop\test2.txt");
string afile = sreader.ReadToEnd();
afile = afile.ToLower();//转换为小写字母
//将空格跟标点符号定义出来
char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' };
//分隔char[c]后产生的数组
string[] S = afile.Split(c);
//建立哈希表
Hashtable ha = new Hashtable();
for (int i = 0; i < S.Length; i++)
{
if (ha.ContainsKey(S[i]))
{
ha[S[i]] = (int)ha[S[i]] + 1;
}
else
{
ha.Add(S[i], 1);
}
}
string[] arrKey = new string[ha.Count];//存键
int[] arrValue = new int[ha.Count];//存值
ha.Keys.CopyTo(arrKey, 0);
ha.Values.CopyTo(arrValue, 0);
Console.WriteLine();
Console.WriteLine(">wf -s test.txt");
Console.WriteLine("total " + ha.Count);
Console.WriteLine();
Array.Sort(arrValue, arrKey);//排序
int n = 0;
for (int i = arrKey.Length - 1; i >= 0; i--)
{
if ((string)arrKey[i] != "")
{
if (n < 10)
{
//输出前10位多的单词,右对齐
Console.Write(arrKey[i].ToString().PadRight(12));
Console.WriteLine(arrValue[i].ToString());
n = n + 1;
}
}
}
}
}
}
}
}

编译一下,却报错了报错如上图:
我很绝望。。。再去查一下资料。。。
先用ptime测试一下功能二的情况,先按照杨老师给的链接下载一个ptime.exe然后用控制台测试,结果如下:

绝望。。。。。。。
听从了老师的建议我请教了其他的同学,修改了代码实现了功能四,以下是我的效能测试:
1.采用ptime.exe测试的3次截图



可以看到的是三次执行时间分别为:1.449秒;0.915秒;0.871秒,取平均值为1.078秒
源码地址:https://coding.net/u/yuanyue2017102885/p/wf-part420170926/git
2.采用vs进行效能测试
浙公网安备 33010602011771号