我在下面用vb.net和visual studio 2015编写了同样的项目。
Visual Basic项目:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim j As Int32 = 0
    '
    Dim Offset As Double = 0D   ' Offset
    Dim Gain As Double = 1D     ' Gain
    Dim Freq As Double = 1D     ' Frequency
    Dim M As Int32 = 3000    ' Number of Block
    Dim N As Int32 = 30000    ' Number of data each block
    Dim delta As Double = 0     ' Sample rate
    Dim DataCount As Int32 = N * M  ' Total data count
    '
    Dim X As Double
    Dim Y As Double
    '
    delta = N / DataCount
    '
    ' Fonksiyon
    '
    Dim Time As Stopwatch
    Time = New Stopwatch
    Time.Start()
    '
    For b As Int32 = 0 To M - 1
        For i As Int32 = 0 To N - 1
            X = j * delta
            Y = Offset + Gain * Math.Cos(X * Freq * Math.PI / 180.0)
            j = j + 1
        Next
    Next b
    '
    Time.Stop()
    Console.WriteLine("Elapsed time :{0}", Time.Elapsed)
End Sub

以及C#项目:
 private void button1_Click(object sender, EventArgs e)
    {

        Int32 j = 0;

        Double Offset = 0D;  // Offset
        Double Gain = 1D;     // Gain
        Double Freq = 1D;     // Frequency
        Int32 M = 3000;    // Number of Block
        Int32 N = 30000;    // Number of data each block
        Double delta = 0;     // Sample rate
        Int32 DataCount = N * M;  // Total data count
        //
        Double X;
        Double Y;
        //
        delta = N / DataCount;
        //
        // Fonksiyon
        //
        Stopwatch Time;
        Time = new Stopwatch();
        Time.Start();
        //
        for (Int32 b=0; b< M; b++) {

            for (Int32 i= 0; i < N; i++) {
            X = j * delta;
            Y = Offset + Gain * Math.Cos(X * Freq * Math.PI / 180.0);
            j = j + 1;
            }
        }
        //
        Time.Stop();
        Console.WriteLine("Elapsed time :{0}", Time.Elapsed);
    }

项目运行了三次,得到了延迟时间。
运行Visual Basic项目时获得的结果;
Elapsed time :00:00:03.9066617
Elapsed time :00:00:03.9165436
Elapsed time :00:00:03.9031542

以及c项目运行时得到的结果;
Elapsed time :00:00:02.4870551
Elapsed time :00:00:02.4931171
Elapsed time :00:00:02.5005793

在同一个框架中产生不同的结果vb.net和C#之间的差别不止一秒。
为什么vb.net这么慢?

最佳答案

这两个代码在这一行有一个重要的区别:

delta = N / DataCount

在VB中,这是算术除法在C中,它是整数除法。
在vb中返回0.00033333...,在c中返回0。
如果将vb代码修复为同时使用整数除法:
delta = N \ DataCount

这两个代码开始显示完全相同的时间。

关于c# - vb.net慢吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53929855/

10-15 02:38