有没有人知道是否有一个.NET Framework等价于交换uint中的字节顺序?
我正在尝试移植一些使用MSFT _byteswap_ulong的自定义c散列代码(相当于App Nsix中的Apples OSSwapInt32或Bswap32)的到c#.我可以手动编写这个函数,但我怀疑它会利用任何编译器优化(例如c/c ++编译器提供难以超越的内在函数我希望运行时与内置函数相同).如果重要,我不关心保留字节序.
我尝试过基于通用的解决方案,但我不相信这会是最佳的.
BitConverter.ToUInt32(BitConverter.GetBytes(g).Reverse().ToArray<byte>(),0);
Run Code Online (Sandbox Code Playgroud)
编辑:
所以我想出了这个特定函数平均在十分钟内调用了多少次(对于我们的一个哈希消费者).此函数被调用10000000000次.因此,我设置了一些微观分析,以查看c代码与下面提供的解决方案(以及上面提出的解决方案)的性能.
C代码运行许多操作(使用内在)大约.在我可靠的笔记本电脑上1500毫秒.我上面提到的c#代码大约运行2689581 ms.一个巨大的差异.Matthew Watson提供的c#代码运行了将近36000毫秒.Caramiriel提出的第一个解决方案在大约115014毫秒内运行,第二个解决方案提供了近36000个.
虽然这些解决方案都没有接近内在调用的速度,但它们比我原来的解决方案要好得多(对于那么多计算,从44分钟到36秒).这对我的应用程序完全可以接受.虽然.NET编译器提供了一些与本机编译器相同的内在功能,但这样会很好.
为了完整性,这里是我的微基准测试的C代码:
#include "stdafx.h"
#include "windows.h"
unsigned long Swap(unsigned int value)
{
return _byteswap_uint64(value);
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int value = 0x01020304;
unsigned long NUMITER = 10000000000;
unsigned long a=0;
unsigned long z=0;
int throwAwayLoopCount = 5;
for (int k = 0; k < throwAwayLoopCount; ++k)
{
a = GetTickCount();
for (unsigned long i = 0; i < NUMITER; ++i) …Run Code Online (Sandbox Code Playgroud) 我可能做错了什么,但不明显.我有以下代码:
namespace test
{
class Program
{
static void Main(string[] args)
{
using (var system = ActorSystem.Create("MySystem"))
{
var testPassRetriever = system.ActorOf<PrintActor>();
var task = testPassRetriever.Ask<PrintActorMsg>(new PrintActorMsg());
// prevent the application from exiting before message is handled
task.Wait();
Console.WriteLine("Finished.");
Console.ReadLine();
}
}
}
class PrintActorMsg{}
class PrintActor : ReceiveActor
{
public PrintActor()
{
Receive<PrintActorMsg>(msg => Console.WriteLine("foo"));
}
}
}// namespace test
Run Code Online (Sandbox Code Playgroud)
问题是Ask返回的Task永远不会完成.其状态保持在等待激活状态."Foo"确实打印在命令行上,所以我知道actor正在处理Print消息.在重写的演员PrintMsg中我应该做些什么来标记完成的任务?