Why is my xll slower than my UDF?

Mar 6, 2015 at 7:23 PM
Edited Mar 6, 2015 at 7:30 PM
Greetings Everyone!
I have been trying to speed up a macro by using xlls, however, it seems is a lot faster with the UDF than with the XLL.
Some data with code profiling demonstrates it

XLL Time for the sub
Proc:module 1 iteration 11.64831 seconds
UDF Time for the sub
Proc:module 1 iteration 4.25986 seconds

It happens on two UDFs that I have converted, factor is abour 2x or 3x slower.
For instance, the XLL function for this is:
    [ExcelFunction(Description="Joins cell values", Category="Example1")]
    public static object RangeJoin(object[,] cells)
    List<string> list = new List<string>();
    foreach (object o in cells)
if ( !(o is ExcelEmpty) )
list.Add(o.ToString()); }

    return string.Join(" ", list.ToArray());
The UDF Function is
Function RangeJoin(Rng As Range) As String
Dim vArr As Variant
Dim v As Variant
vArr = Rng

   RangeJoin = vbNullString

   For Each v In vArr
      RangeJoin = RangeJoin & " " & v
   Next v

End Function
Both were tested for Range(A1:A701) with data and blanks between cells, both work as expected, just XLL is slower.