当前位置: 首页 > news >正文

NLua性能对比:C#注册函数 vs 纯Lua实现

引言

在NLua开发中,我们常面临一个重要选择:将C#函数注册到Lua环境调用,还是直接在Lua中实现逻辑? 直觉告诉我们,C#作为编译型语言性能更高,但跨语言调用的开销是否会影响整体性能?本文通过基准测试揭示真相。


测试场景

实现安全索引访问函数At,对比三种实现方式:

  1. NativeLuaAt:纯Lua实现
  2. CSharpRegisterAt:C#实现并注册到Lua
  3. NativeCSharpAt:纯C#直接调用

测试环境:

  • .NET Framework 4.8.1
  • Intel Core i7-1260P
  • BenchmarkDotNet v0.15.0

性能数据对比

方法平均耗时内存分配
NativeLuaAt6,844 ns288 B
CSharpRegisterAt9,585 ns552 B
NativeCSharpAt106 ns32 B

在这里插入图片描述


结论

出乎意料,直接在Lua中实现逻辑会更快,这里原因可能是将C#函数注册到Lua环境调用涉及到上下文切换等耗时动作。
相关代码见NLuaBenchmarkDotNetTest

代码

Lua代码

--[[
仅用于userdata索引访问函数,C#定义
参数:tbl  : 目标Lua表(数组形式)index: 索引值(支持正负索引)strict: [可选]严格模式,默认false,设为true时额外校验元素连续性
返回值:对应索引位置的元素
异常:类型错误或索引越界时抛出错误
--]]
function At(tbl, index)-- 参数校验阶段 -- 检查第一个参数是否为tableif type(tbl) ~= "userdata" thenerror("bad argument #1 (expected table, got "..type(tbl)..")", 2)end-- 检查索引是否为整数if type(index) ~= "number" or math.floor(index) ~= index thenerror("index must be integer, got "..type(index), 2)end-- 长度计算策略 local len = tbl.Length-- 严格模式下验证表连续性-- 索引转换逻辑 local adjusted_index-- 处理正索引(userdata是 的0-based)if index >= 0 thenadjusted_index = index -- 处理负索引(从末尾倒数)elseadjusted_index = len + index end-- 边界检查与错误处理 -- 有效索引范围:1 ≤ index ≤ lenif adjusted_index < 0 or adjusted_index >= len thenlocal direction = index >=0 and "positive" or "negative"error(string.format("Index %d (%s) out of range [%d, %d]", index, direction, -len, len-1), 2)end-- 最终元素获取 return tbl[adjusted_index]
end

C#代码

        /// <summary>/// 安全索引访问器(支持Lua数组的0-based索引规则)/// </summary>/// <param name="collection">目标集合(支持IList接口的集合)</param>/// <param name="index">索引值(支持负索引倒查)</param>/// <param name="strict">严格模式校验元素连续性</param>/// <returns>索引位置的元素</returns>/// <exception cref="ArgumentNullException">输入集合为空</exception>/// <exception cref="ArgumentException">集合类型不合法或索引无效</exception>public static object At(IEnumerable collection, int index, bool strict = false){// 参数基础校验if (collection == null)throw new ArgumentNullException(nameof(collection), "输入集合不能为null");// 类型安全转换IList list = collection as IList;if (list == null)throw new ArgumentException("输入集合必须实现IList接口", nameof(collection));// 获取有效长度int count = list.Count;if (count == 0)throw new ArgumentException("集合中不包含有效元素", nameof(collection));// 索引转换逻辑int adjustedIndex = index >= 0 ? index : count + index;// 边界校验if (adjustedIndex < 0 || adjustedIndex >= count){string msg = $"索引 {index} 超出有效范围 [{-count}, {count - 1}]";throw new ArgumentOutOfRangeException(nameof(index), msg);}// 严格模式校验if (strict){// 校验是否存在null元素for (int i = 0; i < count; i++){if (list[i] == null){throw new ArgumentException($"严格模式检测到空元素 @ 位置 {i}");}}}return ConvertToDouble(list[adjustedIndex]);}

相关文章:

  • SMT贴片制造流程关键环节解析
  • React从基础入门到高级实战:React 核心技术 - 动画与过渡效果:提升 UI 交互体验
  • Qt基础终结篇:从文件操作到多线程异步UI,深度解析核心要点
  • Excel 操作 转图片,转pdf等
  • 新编辑器编写指南--给自己的备忘
  • 【数据结构】——二叉树堆(下)
  • 【深度学习】7. 深度卷积神经网络架构:从 ILSVRC、LeNet 到 AlexNet、ZFNet、VGGNet,含pytorch代码结构
  • uni-app学习笔记十五-vue3页面生命周期(一)
  • pycharm终端遇不显示虚拟环境的问题
  • 【第1章 基础知识】1.8 在 Canvas 中使用 HTML 元素
  • WPF【11_3】WPF实战-重构与美化(可复用的UI组件)
  • 【AI工具应用】使用 trae 实现 word 转成 html
  • PH热榜 | 2025-05-24
  • 【Linux】shell脚本的常用命令
  • winform LiveCharts2的使用--图表的使用
  • Linux 使用 Docker 安装 Milvus的两种方式
  • 微信小程序的软件测试用例编写指南及示例--性能测试用例
  • 【CSS】CSS 和 SASS 的区别
  • 【平面波导外腔激光器专题系列】用于光纤传感的低噪声PLC外腔窄线宽激光器
  • 【IOS】【OC】【应用内打印功能的实现】如何在APP内实现打印功能,连接本地打印机,把想要打印的界面打印成图片
  • 做产品推广的网站有哪些/网站seo外包公司有哪些
  • 珠海网站建设贵公司/b2b网站平台有哪些
  • 兰州建设局网站公告/镇江网站建设制作公司
  • 怎样用javaweb做网站/广州今天刚刚发生的重大新闻
  • 云南省建设厅网站怎么进不去/百度推广效果
  • 佛山模板网站建设/做seo需要哪些知识