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

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

问题描述

在 VB.NET 中,ByValByRef 哪个用于方法参数更快?

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

回应斯劳的评论 -

哪个在运行时消耗的资源更多?

参数在堆栈上传递.堆栈非常快,因为它的内存分配只是一个指针增量,以保留新的帧"或分配记录".大多数 .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:

  • 引用类型 - ByRef 传递:420 毫秒
  • 引用类型 - 传递 ByVal:382 毫秒
  • 值类型 - ByRef 传递:421 毫秒
  • 值类型 - ByVal 传递:416 毫秒
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 -

  • 引用类型 - ByRef 传递:389 毫秒
  • 引用类型 - 传递 ByVal:349 毫秒
  • 值类型 - ByRef 传递:416 毫秒
  • 值类型 - ByVal 传递:385 毫秒

可以得出结论,传递引用类型(字符串、类)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天全站免登陆