gpt4 book ai didi

c# - Mono SIMD 性能恶化?

转载 作者:行者123 更新时间:2023-11-30 13:23:48 25 4
gpt4 key购买 nike

基准代码:

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, 2.0f, 3.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,2.0f,3.0f,4.0f});
DenseVector b = new DenseVector(new float[]{1.0f,2.0f,3.0f,4.0f});
DenseVector c;

public override void Do() {
c = a + b;
}
}

public sealed class DefaultBenchmark : Benchmark {
Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f);
Vector4 b = new Vector4(1.0f, 2.0f, 3.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 运行时 > Mono 2.10.8(默认)
  • 项目 > 选项 > 构建 > 常规 > 目标框架 > Mono/.NET4.0
  • 项目 > 选项 > 构建 > 编译器 > 常规选项 > 启用优化
  • 项目 > 选项 > 构建 > 编译器 > 常规选项 > 平台目标 > x86
  • 项目 > 选项 > 运行 > 常规 > 参数 > -O=simd

结果:

  • 94.4 纳秒
  • 29.7 纳秒
  • 49.9 纳秒
  • 231595.2 纳秒

最佳答案

我会首先怀疑您的基准基础设施。

可能有几点:

  • 您正在使用 `Stopwatch` 为单个操作计时 - 它没有分辨率
  • 您的计时包括虚函数调用
  • 您的样本量 ( 1000 ) 太小

关于c# - Mono SIMD 性能恶化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8705034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com