StringToHex functionality #3
Replies: 3 comments 1 reply
-
In my head, this was already how it was done, that's where the Thanks for testing the mapping approach and pointing out that it is so much faster, that is good to know and please, go ahead with pull requests, of course, I'm open for such suggestions! I think minimizing string copying as in your last suggestion is also a good practice. Would you like to create a PR or should I fix it? I'm wondering why you are concerned with the performance of this method, as I think it's already more than good enough for all the likely use-cases for it? I'm more concerned with the performance of the |
Beta Was this translation helpful? Give feedback.
-
Honestly, I'm not - I think this method is useful for testing and maybe initializations. But because I'm quite busy, I thought I'd start top-down and it was also easier to start with the smallest method. My goal is to review each and every method, because this is a great library and I want to help as much as I can 😄 plus I hope it becomes standard within the VBA community - I for sure will use it. I will create a PR later today - it would be my first ever BTW and I will learn something new. |
Beta Was this translation helpful? Give feedback.
-
Dealt with in 5c38d40 |
Beta Was this translation helpful? Give feedback.
-
Hi Guido,
I would like to discuss performance of the method and I was not sure if you would like me to do a Pull Request. So, I will discuss here first and then you decide if you want me to do a PR or you want to push yourself.
Issues
Before touching on performance, I want to highlight a few things. Here is the current version of the method:
Firstly, I think we should allow for any type of string as input, not just UT16LE and so:
should beccome:
BTW, the
+ 2
should not be there.For example, something like
StringToHex((StrConv("ABC", vbFromUnicode)))
should return0x414243
which can then be reversed withStrConv(HexToString("0x414243"), vbUnicode)
which returnsABC
.Secondly, I would avoid the concatenation entirely, because it is slow. Obviously, the impact here is negligible as we're only concatenating once but we can still replace:
with:
Performance
We could use a mapping to avoid creating strings in a loop for each character. Something like:
This version of the function is about 6 times faster than the original.
There is another approach, but it turns out it is slower than the one presented above, but I will put it here just for reference:
It's still about 4-5 times faster than the original but about 20% slower than the one using a string map.
Actually, there is one way to improve even further. If we minimize the number of times the result string is being copied then we get an extra 10-20% improvement:
Beta Was this translation helpful? Give feedback.
All reactions