- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
编码测试cpu的运行速度
实验人:
时间时间:
代码来自某大神的博客圈!!
编码工具:VS2010,需要对.NET4.0支持。
编码语言:c#
测试原理:字符串相关度计算是文本处理和数据挖掘中一个不可或缺的方法,Levenshtein Distance可以用来比较两个字符串的相似度,即两个字符串之间的“距离”。这个“距离”其实就是从源字符串变换到目标字符串需要进行的删除、插入和替换的次数。算法简图如下:
即:
随机生成1000个1000长度的字符串,并比较字符串相关度。
实验代码:
C#:
/*比较1000长度字符串的相关度,计算cpu并串行计算时间
*性能比计算为10000d/运算所用时间
*运算时间越短,性能比值越大,cpu性能越高*/
namespace SpeedTest
{
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
internal class Program
{
#region Methods
private static void Main(string[] args)
{
var watch =new Stopwatch();
const long count = 1000;
const int length = 1000;
string comparestring = StringDistance.GenerateRandomString(length);
var strlist = new string[count];
var steps = new int[count];
// prepare string[] for comparison
Parallel.For(0, count, i = strlist[i] = StringDistance.GenerateRandomString(length));
Console.WriteLine(已经生成 + count + 个长度为 + length + 的字符串);
watch.Start();
for (int i = 0; i count; i++)
{
steps[i] = StringDistance.LevenshteinDistance(comparestring, strlist[i]);
}
watch.Stop();
Console.WriteLine(完成非并行计算,耗时(ms)+watch.ElapsedMilliseconds);
Console.WriteLine(性能比 + 100000d/watch.ElapsedMilliseconds);
watch.Reset();
watch.Start();
Parallel.For(
0, count, delegate(long i) { steps[i] = StringDistance.LevenshteinDistance(comparestring, strlist[i]); });
watch.Stop();
Console.WriteLine(完成并行计算,耗时(ms) + watch.ElapsedMilliseconds);
Console.WriteLine(性能比 + 100000d / watch.ElapsedMilliseconds);
Console.ReadKey();
}
#endregion
}
internal class StringDistance
{
#region Public Methods
public static string GenerateRandomString(int length)
文档评论(0)