using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.linq;using Mono.Simd;using MathNet.Numerics.linearalgebra.Single;namespace XXX {public static class TimeSpanExtensions { public static double TotalNanoseconds(this TimeSpan timeSpan) { return timeSpan.TotalMilliseconds * 1000000.0; }}public sealed class SimdBenchmark : Benchmark { Vector4f a = new Vector4f(1.0f,2.0f,3.0f,4.0f); Vector4f b = new Vector4f(1.0f,4.0f); Vector4f c; public overrIDe voID Do() { c = a + b; }}public sealed class MathNetBenchmark : Benchmark { DenseVector a = new DenseVector(new float[]{1.0f,4.0f}); DenseVector b = new DenseVector(new float[]{1.0f,4.0f}); DenseVector c; public overrIDe voID Do() { c = a + b; }}public sealed class DefaultBenchmark : Benchmark { Vector4 a = new Vector4(1.0f,4.0f); Vector4 b = new Vector4(1.0f,4.0f); Vector4 c; public overrIDe voID Do() { c = a + b; }}public sealed class SimpleBenchmark : Benchmark { float a = 1.0f; float b = 2.0f; float c; public overrIDe voID Do() { c = a + b; }}public sealed class DelegateBenchmark : Benchmark { private Readonly Action _action; public DelegateBenchmark(Action action) { _action = action; } public overrIDe voID Do() { _action(); }}public abstract class Benchmark : IEnumerable<TimeSpan> { public IEnumerator<TimeSpan> GetEnumerator() { Do(); // warm-up! GC.Collect(); // Collect garbage. GC.WaitForPendingFinalizers(); // Wait until finalizers finish. var stopwatch = new Stopwatch(); while (true) { stopwatch.reset(); stopwatch.Start(); Do(); stopwatch.Stop(); yIEld return stopwatch.Elapsed; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public abstract voID Do();}public struct Vector4 { float x; float y; float z; float w; public Vector4(float x,float y,float z,float w) { this.x = x; this.y = y; this.z = z; this.w = w; } public static Vector4 operator +(Vector4 v1,Vector4 v2) { return new Vector4(v1.x + v2.x,v1.y + v2.y,v1.z + v2.z,v1.w + v2.w); }}class MainClass { public static voID Main(string[] args) { var avgNS1 = new SimdBenchmark().Take(1000).Average(timeSpan => timeSpan.TotalNanoseconds()); var avgNS2 = new SimpleBenchmark().Take(1000).Average(timeSpan => timeSpan.TotalNanoseconds()); var avgNS3 = new DefaultBenchmark().Take(1000).Average(timeSpan => timeSpan.TotalNanoseconds()); var avgNS4 = new MathNetBenchmark().Take(1000).Average(timeSpan => timeSpan.TotalNanoseconds()); Console.Writeline(avgNS1 + " ns"); Console.Writeline(avgNS2 + " ns"); Console.Writeline(avgNS3 + " ns"); Console.Writeline(avgNS4 + " ns"); }}}
环境设置:
windows 7 / Mono 2.10.8 / MonoDevelop 2.8.5
MonoDevelop设置:
>工具>选项> .NET运行时>单声道2.10.8(默认)
>项目>选项>构建>一般>目标框架>单声道/ .NET
4
>项目>选项>构建>编译器>一般选项>启用优化
>项目>选项>构建>编译器>一般选项>平台目标> 86
>项目>选项>运行>一般>参数> -O = SIMD
结果:
> 94.4 ns
> 29.7 ns
> 49.9 ns
> 231595.2 ns
有几点可能是:
>您正在使用“秒表”计时单次 *** 作 – 它没有分辨率>您的计时包括虚拟函数调用>您的样本量(1000)太小
总结以上是内存溢出为你收集整理的c# – Mono SIMD性能恶化?全部内容,希望文章能够帮你解决c# – Mono SIMD性能恶化?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)