성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - byte 배열을 Hex(16진수) 문자열로 고속 변환하는 방법</h1> <p> 재미있는 답변이 있군요. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How do you convert a byte array to a hexadecimal string, and vice versa? ; <a target='tab' href='https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa'>https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa</a> </pre> <br /> 위의 글에 보면 byte 배열의 값을 각각 <a target='tab' href='https://www.sysnet.pe.kr/2/0/11913'>16진수 문자열로 변환</a>하는 다양한 방법에 대해 성능을 비교한 덧글을 볼 수 있습니다. 그중에서 가장 빠른 방법이 "Lookup by byte unsafe (via CodesInChaos)"라고 소개하는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How do you convert a byte array to a hexadecimal string, and vice versa? - Lookup by byte unsafe (via CodesInChaos) ; <a target='tab' href='https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727'>https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private static readonly uint[] _lookup32Unsafe = CreateLookup32Unsafe(); private static readonly uint* _lookup32UnsafeP = (uint*)GCHandle.Alloc(_lookup32Unsafe,GCHandleType.Pinned).AddrOfPinnedObject(); private static uint[] CreateLookup32Unsafe() { var result = new uint[256]; for (int i = 0; i < 256; i++) { string s=i.ToString("X2"); if(BitConverter.IsLittleEndian) result[i] = ((uint)s[0]) + ((uint)s[1] << 16); else result[i] = ((uint)s[1]) + ((uint)s[0] << 16); } return result; } public static string ByteArrayToHexViaLookup32Unsafe(byte[] bytes) { var lookupP = _lookup32UnsafeP; var result = new char[bytes.Length * 2]; fixed(byte* bytesP = bytes) fixed (char* resultP = result) { uint* resultP2 = (uint*)resultP; for (int i = 0; i < bytes.Length; i++) { resultP2[i] = lookupP[bytesP[i]]; } } return new string(result); } </pre> <br /> 동적 프로그래밍을 할 때도 마찬가지고, 언제나 성능은 cache가 정답으로 보입니다. ^^ (혹시 저 소스 코드보다 더 빠르게 최적화하신 분이 계실까요? ^^)<br /> <br /> 실제로 비교를 한 번 해보겠습니다. 우선, 코드가 간단해서 우리가 흔히 쓰는 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter'>BitConverter</a>를 이용한 방법과,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // BitConverter 버전 BitConverter.ToString(buf).Replace("-", ""); </pre> <br /> 아무래도 저건 루프를 두 번 돌 테니 직접 만들어서 구현한 코드를 놓고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ToHex 버전 StringBuilder sb = new StringBuilder(buf.Length * 2); foreach (byte b in buf) { sb.Append(b.ToString("x2")); } return sb.ToString(); </pre> <br /> 함께 비교해 보면 다음과 같은 성능 수치를 확인할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // x64 + Release 빌드, 8192 바이트에 대해 10,000 회 테스트 BitConverter : 1153 ToHex : 4738 UnsafeLookup : 91 </pre> <br /> 오호... 의외군요, StringBuilder를 이용해 루프를 한 번 돌도록 만든 "ToHex" 버전보다 BitConverter가 더 빠릅니다. 물론, UnsafeLookup은 압도적으로 빠르고. ^^<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, ToHex 버전을 StringBuilder를 사용하지 않고 BitConverter의 내부 코드를 조금 인용해 다음과 같이 만들어 볼 수도 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > char[] text = new char[buf.Length * 2]; int srcPos = 0; for (int dstPos = 0; dstPos < text.Length; dstPos += 2) { byte b = buf[srcPos++]; text[dstPos] = GetHexValue(((int)b) / 16); text[dstPos + 1] = GetHexValue(((int)b) % 16); } return new string(text); static char GetHexValue(int number) { if (number < 10) { return (char)(number + 48); } return (char)(number - 10 + 65); } </pre> <br /> 그럼 BitConverter보다 성능이 (당연히) 더 좋습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > BitConverter : 1164 ToHex : 240 UnsafeLookup : 96 </pre> <br /> (그러니까, <a target='tab' href='https://www.sysnet.pe.kr/2/0/1705'>괜히 코드를 어설프게 만들면 마이크로소프트 측에서 만든 BitConverter보다 못한 성능</a>을 내는 것입니다. ^^)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 아래는 이 글에서 테스트한 전체 소스 코드입니다. (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1741&boardid=331301885'>첨부 파일로 프로젝트를 올려 두었습니다.</a>)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; class Program { static void Main(string[] args) { Action<int, string, Action<int, byte[]>, byte[]> action = (loopCount, title, work, arg) => { Stopwatch st = new Stopwatch(); st.Start(); work(loopCount, arg); st.Stop(); Console.WriteLine(title + " : " + st.ElapsedMilliseconds); }; action(1, "BitConverter", UseBitConverter, new byte[] { 0 }); action(1, "ToHex", ToHex, new byte[] { 0 }); action(1, "UnsafeLookup", UnsafeLookup, new byte[] { 0 }); Console.WriteLine(); action(10000, "BitConverter", UseBitConverter, new byte[8192]); action(10000, "ToHex", ToHex, new byte[8192]); action(10000, "UnsafeLookup", UnsafeLookup, new byte[8192]); } private static void UseBitConverter(int loopCount, byte[] buf) { for (int i = 0; i < loopCount; i++) { BitConverter.ToString(buf).Replace("-", ""); } } static string ConvertWithStringBuilder(byte[] buf) { StringBuilder sb = new StringBuilder(buf.Length * 2); foreach (byte b in buf) { sb.Append(b.ToString("x2")); } return sb.ToString(); } static string ConvertToHex(byte[] buf) { char[] text = new char[buf.Length * 2]; int srcPos = 0; for (int dstPos = 0; dstPos < text.Length; dstPos += 2) { byte b = buf[srcPos++]; text[dstPos] = GetHexValue(((int)b) / 16); text[dstPos + 1] = GetHexValue(((int)b) % 16); } return new string(text); } static char GetHexValue(int number) { if (number < 10) { return (char)(number + 48); } return (char)(number - 10 + 65); } private static void ToHex(int loopCount, byte [] buf) { for (int i = 0; i < loopCount; i ++) { ConvertToHex(buf); // ConvertWithStringBuilder(buf); } } private static void UnsafeLookup(int loopCount, byte[] buf) { for (int i = 0; i < loopCount; i++) { ByteToHex.ByteArrayToHexViaLookup32Unsafe(buf); } } } public unsafe class ByteToHex { private static readonly uint[] _lookup32Unsafe = CreateLookup32Unsafe(); private static readonly uint* _lookup32UnsafeP = (uint*)GCHandle.Alloc(_lookup32Unsafe, GCHandleType.Pinned).AddrOfPinnedObject(); private static uint[] CreateLookup32Unsafe() { var result = new uint[256]; for (int i = 0; i < 256; i++) { string s = i.ToString("X2"); if (BitConverter.IsLittleEndian) result[i] = ((uint)s[0]) + ((uint)s[1] << 16); else result[i] = ((uint)s[1]) + ((uint)s[0] << 16); } return result; } public static string ByteArrayToHexViaLookup32Unsafe(byte[] bytes) { var lookupP = _lookup32UnsafeP; var result = new char[bytes.Length * 2]; fixed (byte* bytesP = bytes) fixed (char* resultP = result) { uint* resultP2 = (uint*)resultP; for (int i = 0; i < bytes.Length; i++) { resultP2[i] = lookupP[bytesP[i]]; } } return new string(result); } } </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1395
(왼쪽의 숫자를 입력해야 합니다.)