哪个更快? ByVal或ByRef? [英] Which is faster? ByVal or ByRef?

查看:159
本文介绍了哪个更快? ByVal或ByRef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中, ByVal ByRef 更快,用于方法参数?

In VB.NET, which is faster to use for method arguments, ByVal or ByRef?

还需要在运行时(RAM)消耗更多资源吗?

Also, which consumes more resources at runtime (RAM)?

我阅读了这个问题,但是答案并不适用或不够具体。

I read through this question, but the answers are not applicable or specific enough.

推荐答案

Byval和ByRef参数应根据要求和对它们如何工作而不是在速度上的了解而使用。

Byval and ByRef arguments should be used based on requirements and knowledge of how they work not on speed.

http://www.developer.com/net/vb/article.php/3669066

回应Slough的评论-

哪些消费量更多

参数在堆栈上传递。堆栈非常快,因为其内存分配只是指针增量,以保留新的帧或分配记录。多数.NET参数不会超过机器寄存器的大小,因此,如果使用任何堆栈空间来传递参数,则其大小将很小。实际上,基本类型和指针都在堆栈上分配。 .NET中的堆栈大小限制为1 MB。这应该让您了解参数传递消耗了多少资源。

Parameters are passed on the stack. The stack is very fast, because its memory allocation is simply a pointer increment to reserve a new "frame" or "allocation record." Most .NET parameters do not exceed the size of a machine register so little if any "stack" space is used to pass parameters. In fact basic types and pointers are both allocated on the stack. The stack size in .NET is limited to 1 MB. This should give you an idea of how few resources are consumed by parameter passing.

您可能会发现这一系列文章很有趣:

You may find this series of articles interesting:

通过堆栈分配提高性能(.NET内存管理:第2部分)

哪个更快? ByVal或ByRef。

充其量也很难准确,准确地测量-取决于您的测量环境,但是我写了一个基准测试方法1亿次出现了以下内容:

It is difficult at best to measure accurately and fairy - depending on the context of your measurement, but a benchmark I wrote calling a method 100 million times came up with the following:


  • 引用类型-通过引用传递的:420 ms

  • 引用类型-通过ByVal:382 ms

  • 值类型-通过ByRef:421 ms

  • 值类型-通过ByVal:416 ms

  • Reference Type - Passed ByRef: 420 ms
  • Reference Type - Passed ByVal: 382 ms
  • Value Type - Passed ByRef: 421 ms
  • Value Type - Passed ByVal: 416 ms
Public Sub Method1(ByRef s As String)
    Dim c As String = s
End Sub

Public Sub Method2(ByVal s As String)
    Dim c As String = s
End Sub

Public Sub Method3(ByRef i As Integer)
    Dim x As Integer = i
End Sub

Public Sub Method4(ByVal i As Integer)
    Dim x As Integer = i
End Sub

Sub Main()

    Dim s As String = "Hello World!"
    Dim k As Integer = 5

    Dim t As New Stopwatch

    t.Reset()
    t.Start()
    For i As Integer = 0 To 100000000
        Method1(s)
    Next
    t.Stop()

    Console.WriteLine("Reference Type - ByRef " & t.ElapsedMilliseconds)

    t.Reset()
    t.Start()
    For i As Integer = 0 To 100000000
        Method2(s)
    Next
    t.Stop()

    Console.WriteLine("Reference Type - ByVal " & t.ElapsedMilliseconds)

    t.Reset()
    t.Start()
    For i As Integer = 0 To 100000000
        Method3(i)
    Next
    t.Stop()

    Console.WriteLine("Value Type - ByRef " & t.ElapsedMilliseconds)

    t.Reset()
    t.Start()
    For i As Integer = 0 To 100000000
        Method4(i)
    Next
    t.Stop()

    Console.WriteLine("Value Type - ByVal " & t.ElapsedMilliseconds)

    Console.ReadKey()

End Sub

注释变量和赋值在每种方法中-

Commenting out the variable and assignment in each method -


  • 引用类型-通过引用:389 ms

  • 引用类型-通过ByVal :349 ms

  • 值类型-通过ByRef:416 ms

  • 值类型-通过ByVal:385 ms

  • Reference Type - Passed ByRef: 389 ms
  • Reference Type - Passed ByVal: 349 ms
  • Value Type - Passed ByRef: 416 ms
  • Value Type - Passed ByVal: 385 ms

可以得出结论,传递引用类型(字符串,类)ByVal可以节省一些时间。您可能还会说,传递值类型(整数,字节)-ByVal将节省一些时间。

One could conclude that passing reference types (strings, classes) ByVal will save some time. You might also say that passing value types (integer, byte) - ByVal will save some time.

同样,在宏大的事情中,时间可以忽略不计。更重要的是正确使用ByVal和ByRef并了解幕后情况。您的例程中实现的算法最有可能肯定会多次影响程序的运行时间。

Again the time is negligible in the grand scheme of things. What's more important is using ByVal and ByRef properly and understanding what's going on "behind the scenes." The algorithms implemented in your routines will most surely affect the runtime of your program many times more.

这篇关于哪个更快? ByVal或ByRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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