令人惊讶的结果 [英] Surprising results

查看:80
本文介绍了令人惊讶的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,如果有人问以下两种编码方法中的哪一种将b / b $ b $整数编码成一个字节数组更快。


目的是为作为

''标题''的一部分发送到套接字。

如果您好奇,请自行运行代码。


if(args.Length!= 1)

{

Console.WriteLine(" Usage:{0}< iterations>",

System.AppDomain.CurrentDomain.FriendlyName);


返回;

}


int [ ] size = new int [Convert.ToInt32(args [0])];

for(int i = 0; i< size.Length; i ++)

size [i] = i;


HiPerfTimer计时器=新的HiPerfTimer();


byte [] headerBuf = null;

timer.Start();

for(int i = 0; i< size.Length; i ++)

{

//将int转换为网络字节中的字节数组

order

headerBuf = BitConverter.GetBytes(size [i]);

}
timer.Stop();

long bitConvert = timer.usec;


byte [] bas = null;

timer.Start();

for(int i = 0;我< size.Length; i ++)

{

bas = new byte [4];

bas [0] =(byte)(size [i]> > 24);

bas [1] =(byte)(size [i]>> 16);

bas [2] =(byte)( size [i]>> 8);

bas [3] =(byte)(size [i]);

}

timer.Stop();

long bitShift = timer.usec;


Console.WriteLine("长度为{0}的样本的BitShift方法

花了{1}微秒,size.Length,bitShift);

Console.WriteLine(长度样本的BitConverter方法

{ 0}花费{1}微秒,size.Length,bitConvert);

Imagine if someone asked which of the follwing two methods of encoding
an integer into a byte array was quicker.

The purpose is in preparation for sending across a socket as part of a
''header''.
If you are curious, run the code yourself.

if(args.Length != 1)
{
Console.WriteLine("Usage: {0} <iterations>",
System.AppDomain.CurrentDomain.FriendlyName);

return;
}

int[] size = new int[Convert.ToInt32(args[0])];
for(int i = 0; i < size.Length; i++)
size[i] = i;

HiPerfTimer timer = new HiPerfTimer();

byte[] headerBuf = null;
timer.Start();
for(int i = 0; i < size.Length; i++)
{
// convert the int to a byte array in network byte
order
headerBuf = BitConverter.GetBytes(size[i]);
}
timer.Stop();
long bitConvert = timer.usec;

byte[] bas = null;
timer.Start();
for(int i = 0; i < size.Length; i++)
{
bas = new byte[4];
bas[0] = (byte)(size[i] >> 24);
bas[1] = (byte)(size[i] >> 16);
bas[2] = (byte)(size[i] >> 8);
bas[3] = (byte)(size[i]);
}
timer.Stop();
long bitShift = timer.usec;

Console.WriteLine("BitShift method on sample of length {0}
took {1} microseconds", size.Length, bitShift );
Console.WriteLine("BitConverter method on sample of length
{0} took {1} microseconds", size.Length, bitConvert );

推荐答案

经过100 mil迭代后,BitConverter得到23秒只有17美元才能转移




另外,为了获得更精确的结果(差别不大),我更改了网址

转换成一个类似于GetBytes声明的函数

所以它不能说时间上的差异是

函数调用的开销。如果有差异,那就非常小了。


Mythran


" Jack" < JA ** @ yankeeboysoftware.com>在消息中写道

news:bb ************************** @ posting.google.c om ...
After 100 mil iterations, I get 23 seconds for the BitConverter and only 17 for
the bit shifting.

Also, for more precise results (which made little difference), I changed thebit
conversion portion into a function which is similar to the GetBytes declaration
so that it couldn''t be said the the difference in time is the overhead for
function calls. If there was a difference, it is VERY VERY small.

Mythran

"Jack" <ja**@yankeeboysoftware.com> wrote in message
news:bb**************************@posting.google.c om...
想象一下,如果有人问以下两种编码整数到字节数组的方法中的哪一种更快。

目的是准备发送一个整数socket作为
''header''的一部分。

如果你很好奇,自己运行代码。

if(args.Length!= 1)
{
Console.WriteLine(" Usage:{0}< iterations>",
System.AppDomain.CurrentDomain.FriendlyName);

返回;
}
int [] size = new int [Convert.ToInt32(args [0])];
for(int i = 0; i< size.Length; i ++)
size [i] = i;

HiPerfTimer timer = new HiPerfTimer();

byte [] headerBuf = null;
计时器。 ()。开始();
i = 0; i< size.Length; i ++)
//将int转换为网络字节中的字节数组
headerBuf = BitConverter.GetBytes(size [i]);
}
timer.Stop();
long bitConvert = timer.usec;

byte [] bas = null;
timer.Start();
for (int i = 0; i< size.Length; i ++)
{
bas = new byte [4];
bas [0] =(byte)(size [i]> ;> 24);
bas [1] =(byte)(size [i]>> 16);
bas [2] =(byte)(size [i]>> ; 8);
bas [3] =(byte)(size [i]);
}
timer.Stop();
long bitShift = timer.usec; < />
Console.WriteLine(长度为{0}的样本上的BitShift方法花了{1}微秒,size.Length,bitShift);
Console.WriteLine(") BitConverter m长度样本的方法
{0}花了{1}微秒,size.Length,bitConvert);
Imagine if someone asked which of the follwing two methods of encoding
an integer into a byte array was quicker.

The purpose is in preparation for sending across a socket as part of a
''header''.
If you are curious, run the code yourself.

if(args.Length != 1)
{
Console.WriteLine("Usage: {0} <iterations>",
System.AppDomain.CurrentDomain.FriendlyName);

return;
}

int[] size = new int[Convert.ToInt32(args[0])];
for(int i = 0; i < size.Length; i++)
size[i] = i;

HiPerfTimer timer = new HiPerfTimer();

byte[] headerBuf = null;
timer.Start();
for(int i = 0; i < size.Length; i++)
{
// convert the int to a byte array in network byte
order
headerBuf = BitConverter.GetBytes(size[i]);
}
timer.Stop();
long bitConvert = timer.usec;

byte[] bas = null;
timer.Start();
for(int i = 0; i < size.Length; i++)
{
bas = new byte[4];
bas[0] = (byte)(size[i] >> 24);
bas[1] = (byte)(size[i] >> 16);
bas[2] = (byte)(size[i] >> 8);
bas[3] = (byte)(size[i]);
}
timer.Stop();
long bitShift = timer.usec;

Console.WriteLine("BitShift method on sample of length {0}
took {1} microseconds", size.Length, bitShift );
Console.WriteLine("BitConverter method on sample of length
{0} took {1} microseconds", size.Length, bitConvert );



>经过100 mil迭代后,BitConverter得到23秒,而
> After 100 mil iterations, I get 23 seconds for the BitConverter and only 17 for
只有17次换位。



这也是我的结果所示。我很惊讶

可能会有这样的差异,这让我想知道

BitConverter函数在做什么。


This is what my results indicated as well. I am surprised that there
could be such a difference, which makes me wonder what the
BitConverter function is doing.




" Jack" < JA ** @ yankeeboysoftware.com>在消息中写道

news:bb ************************** @ posting.google.c om ...

"Jack" <ja**@yankeeboysoftware.com> wrote in message
news:bb**************************@posting.google.c om...
想象一下,如果有人问以下两种编码整数到字节数组的方法中的哪一种更快。

目的是准备发送一个整数套接字作为
''标题''的一部分。
Imagine if someone asked which of the follwing two methods of encoding
an integer into a byte array was quicker.

The purpose is in preparation for sending across a socket as part of a
''header''.




两件事:


1)当你发布这样的代码,发布整个该死的文件。不得不写出来它是一件很痛苦的事情,特别是当你必须猜测

使用哪些命名空间时,这导致我

2当您使用不属于框架的程序集或类时,

*始终*提及它并提供链接(或在适当时包括它)。它b / b
让人们更少。 HiPerfTimer我发现*不能和你的代码一起使用,所以

我放弃这一点并继续前进。



Two things:

1) When you post code like this, post the whole damn file. Having to write
it out is a pain, especially when you have to guess what namespaces where in
use, which leads me to
2) When you use an assembly or class that isn''t part of the framework,
*ALWAYS* mention it and provide a link(or include it if appropriate). It
irks people less. The HiPerfTimer I found *doesn''t* work with your code, so
for the moment I am giving up on this and moving on.


这篇关于令人惊讶的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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