布尔数组位操作优化
以下是针对布尔数组位操作优化的系统性、工业级总结专门为你当前的 16 位错误码 LabVIEW 反转逻辑场景设计。一、优化目标将uint errorCode → bool[16] → Reverse → 循环检查这个流程优化为高性能、低分配、可维护的实现。二、优化方案对比表推荐顺序方案名称内存分配性能可读性维护性推荐场景1零分配直接位测试无★★★★★★★★☆☆★★★★★工业首选2SWAR 位反转 正序循环无★★★★☆★★★★★★★★★★团队维护推荐3查找表 (Lookup Table)64KB 静态★★★★★★★★☆☆★★★★☆极致高频调用4bool[] Array.Reverse有多次★☆☆☆☆★★★★★★★★★★仅用于学习/调试三、最佳实现代码强烈推荐方案1零分配直接位测试最高性能 推荐/// summary/// 布尔数组位操作最高性能优化版 - 零分配直接位测试/// 严格对应 LabVIEW: Number To Boolean Array Reverse 1D Array/// /summarypublicstringGetAllErrorMessages(stringdeviceType,objecterrorValue){if(string.IsNullOrWhiteSpace(deviceType))return错误设备类型不能为空;deviceTypedeviceType.Trim();uinterrorCodeParseErrorCode(errorValue);// 支持十/十六进制varsbnewStringBuilder(512);if(_directDeviceTypes.Contains(deviceType)){// 直接Code模式if(_directErrors.TryGetValue(deviceType,outvardict)dict.TryGetValue((int)errorCode,outstring?msg)){sb.Append($错误码:{errorCode}(0x{errorCode:X4}){msg});}else{sb.Append($未知错误码:{errorCode}(0x{errorCode:X4}));}}elseif(_bitErrors.TryGetValue(deviceType,outvarerrorList)){boolhasErrorfalse;// 核心优化零分配直接位判断 // 对应 LabVIEW 反转后逻辑i0 对应原始 bit15最高位for(inti0;i16ierrorList.Count;i){if((errorCode(1u(15-i)))!0){var(code,source)errorList[i];if(hasError)sb.AppendLine();sb.Append($错误码:{code}{source});hasErrortrue;}}if(!hasErrorerrorCode!0){sb.Append($错误码:{errorCode}(0x{errorCode:X4}) 有置位但未定义信息);}}else{sb.Append($不支持的设备类型:{deviceType});}returnsb.ToString();}方案2SWAR 位反转版逻辑最清晰[MethodImpl(MethodImplOptions.AggressiveInlining)]privatestaticuintReverseBits16(uintx){x((x0x5555u)1)|((x0xAAAAu)1);x((x0x3333u)2)|((x0xCCCCu)2);x((x0x0F0Fu)4)|((x0xF0F0u)4);x((x0x00FFu)8)|((x0xFF00u)8);returnx;}// 使用方式uintreversedReverseBits16(errorCode);for(inti0;i16ierrorList.Count;i){if((reversed(1ui))!0)// 正序判断逻辑非常直观{// 输出 errorList[i]}}四、完整推荐类已集成最高性能优化我推荐你直接使用下面这个零分配最高性能版本usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Text;usingSystem.Globalization;namespaceIndustrialErrorProcessor{publicclassErrorCodeProcessor:IDisposable{// ... 字段定义同之前_directErrors, _bitErrors, _directDeviceTypes, _bitDeviceTypespublicErrorCodeProcessor(stringcsvFilePath){Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);LoadCsv(csvFilePath);}// LoadCsv 方法保持你之前可用的版本支持多编码防乱码/// summary/// 布尔数组位操作最高性能优化版零分配/// /summarypublicstringGetAllErrorMessages(stringdeviceType,objecterrorValue){deviceTypedeviceType?.Trim()??;uinterrorCodeParseErrorCode(errorValue);varsbnewStringBuilder(512);if(_directDeviceTypes.Contains(deviceType)){if(_directErrors.TryGetValue(deviceType,outvardict)dict.TryGetValue((int)errorCode,outstring?msg))sb.Append($错误码:{errorCode}(0x{errorCode:X4}){msg});elsesb.Append($未知错误码:{errorCode}(0x{errorCode:X4}));}elseif(_bitErrors.TryGetValue(deviceType,outvarerrorList)){boolhasErrorfalse;// 最高性能零分配实现for(inti0;i16ierrorList.Count;i){if((errorCode(1u(15-i)))!0){var(code,source)errorList[i];if(hasError)sb.AppendLine();sb.Append($错误码:{code}{source});hasErrortrue;}}if(!hasErrorerrorCode!0)sb.Append($错误码:{errorCode}(0x{errorCode:X4}) 有置位但未定义信息);}else{sb.Append($不支持的设备类型:{deviceType});}returnsb.ToString();}[MethodImpl(MethodImplOptions.AggressiveInlining)]privatestaticuintParseErrorCode(object?input){if(inputnull)return0;stringsinput.ToString()?.Trim()??;if(s.StartsWith(0x,StringComparison.OrdinalIgnoreCase))ss[2..];returnuint.TryParse(s,NumberStyles.HexNumber,null,outuinth)?h:uint.TryParse(s,outuintd)?d:0u;}publicvoidDispose(){_directErrors.Clear();_bitErrors.Clear();}// ErrorRow 类定义...}}最终建议对于你的工业项目优先使用「零分配直接位测试」方案第一个代码块。它在性能、可维护性和内存使用上达到了最佳平衡。如果你希望我提供包含完整LoadCsv的全类代码SWAR 版本完整类BenchmarkDotNet 测试对比代码
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2566204.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!