C # function normal return value VS out or ref argument

I have a C # method that should return a very large array (or any other large data structure, for that matter).

Is there a performance gain when using the ref or out option instead of the default return value?

those. is there any performance or other advantage when using

void function(sometype input, ref largearray) 

above

 largearray function(sometype input) 
+6
c # return arguments
source share
4 answers

The amount of stack space used on an x86 32-bit processor to pass arguments of various types:

  • byte: 4 bytes
  • bool: 4 bytes
  • enumeration: 4 bytes
  • char: 4 bytes
  • short: 4 bytes
  • int: 4 bytes
  • long: 8 bytes
  • float: 4 bytes
  • double: 8 bytes
  • decimal: 16 bytes
  • struct: structure runtime

  • string: 4 bytes
  • array: 4 bytes
  • object: 4 bytes
  • interface: 4 bytes
  • pointer: 4 bytes
  • class instance: 4 bytes

Those below the line are reference types; their size is doubled on a 64-bit processor.

To call the static method, the first 2 arguments, which contain up to 4 bytes, will be passed through the CPU registers, and not onto the stack. To invoke an instance method, only one argument will be passed through registers. The rest are passed on the stack. The 64-bit processor supports passing 4 arguments through registers.

As you can see from the list, the only time you should consider passing a ref argument is for structures. The normal guide for this is to do this when the structure is larger than 16 bytes. It is not always easy to guess the size of the execution time of the structure, usually up to 4 fields. Less if these fields are double, long or decimal. This rule usually recommends turning your structure into a class for this reason.

Also note that there is no saving that passes an argument as byte or short intentionally, int is the type that a 32-bit processor satisfies. The same goes for the currently available 64-bit processors.


The return value of the method, the real topic of your question is almost always returned in the CPU register. Most types are conveniently embedded in the eax or edx registers: eax, the FPU register for floating point values. The only exceptions are large structures and decimal numbers; they are too large to be case-sensitive. They are called by reserving the stack space for the return value and passing a 4-byte pointer to this space as an argument to the method.

+5
source share

The out parameter returns a reference to an instance of a type that does not need to be initialized before sending to the method.

Parameter

A ref returns a reference to an instance of the type that must be initialized before sending to the method.

This is about call semantics, not performance.

+1
source share

No, just return the array

+1
source share

There would be no difference between

 void function(sometype input, out largearray output ) 

above

 largearray function(sometype input) 

However, if you do

 largearray function( sometype input, ref largearray output ) 

and you require the caller to allocate a large array, which of course will be faster, but that will make a difference if you call the method again and save the large array allocated between calls.

+1
source share

All Articles