c# - 为什么我的 XLL 比我的 UDF 慢?

标签 c# excel excel-dna xll excel-udf

我一直在尝试通过使用 XLL 来加速宏,但是,使用 UDF 似乎比使用 XLL 快得多。

一些带有代码分析的数据证明了这一点

XLL 子时间 Proc:模块 1 迭代 11.64831 秒

UDF 子时间 Proc:模块 1 迭代 4.25986 秒

它发生在我转换的两个 UDF 上,因子大约慢 2 倍或 3 倍。 例如,XLL 函数是:

[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());

}

UDF函数是

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

两者都针对 Range(A1:A701) 进行了测试,单元格之间有数据和空白,两者都按预期工作,只是 XLL 较慢。

最佳答案

  1. VBA 直接访问单元格内容。它使用其 native 类型,并且 99% 的情况下,编码良好的 UDF 将比执行相同操作的外部库更快。

  2. 在您的示例中,您在 C# 版本中做了很多事情。

  3. 如果你真的想提高性能并利用 C# 的能力来做一些事情而无需循环 *你应该考虑passing a Range object to your external library pretending like you're actually passing a 1D array .在 C# 中接收它,你就可以使用所有很酷的东西,比如 LINQ .

关于c# - 为什么我的 XLL 比我的 UDF 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28957030/

相关文章:

c# - 为什么我的 CollectionView 向下或向上滚动时速度很慢?如何解决性能问题?我已经清理了我的 UI 代码

c# - 将 DataGrid 导出到 CSV 或 Excel

c# - 检测 xll 是否已永久安装或 xll 文件是否只是临时打开?

c# - 基于类设计生成类 - 开源工具?

c# - 如何使用 MVC Html Helpers 截断字符串?

r - 如何将 Unicode 输出写入 .csv 以在 Excel 中使用?

c# - 如何根据 UsedRange 剪辑 ExcelReference?

c# - 打开加载项时出现诊断显示

c# - 不变的词典

arrays - 数组公式的多个条件