Subtraction v Hex2Dec in vbscript

Bob Schneider
Bob Schneider used Ask the Experts™
on
Lets say I had to iterate through a loop of 1000 rows of a text file and at each row I had to take a value and do one of the following:
1) If the value were a decimal value with 6 digits, subtract 100,000 from the value
2) If the value were a hex value, convert it to decimal.

Which would be quicker/use fewer resources?

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You can use my article on how to time code to test it, but something like this is probably the fastest.

MyLong = CLng("&h" & HexString)
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
I don't think there would be a significant different over a 1000 record file, or a reasonably current computer.  That being said, I'd probably need a little more to go on, like what assumptions can be made, and what level of protection is needed.  Meaning, can there be bad data, and how is it to be nadled - like numbers with more or less than 6 digits, text strings that have spaces or characters outside the 0-F range,  etc.  Will the input always be only one of the two types you mentioned, or will the code have to determine the class of the data on each line and then act accordingly?

I guess as always, the more info you can share the better the responses here.

~bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
Here's a simple test, save as a VBS and run with CSCRIPT.  I had to go a lot higher than 1000 iterations to get any significant time as you can see.  The subtraction seems to take about 60% of the time converting hex to Long does, at least with this crude data.

max = 10000000

t1 = Timer()
For i = 1 to max
    d = 999999 - 100000
Next
t2 = Timer()
Wscript.Echo t2-t1

t1 = Timer()
For i = 1 to max
    h = Clng(&hFFFF)
Next
t2 = Timer()
Wscript.Echo t2-t1

Open in new window

~bp
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
Making it work a little harder, this statement takes longer, so for me each test takes about 1 second...

d = 100000 - 1

~bp
Bob SchneiderCo-Owner

Author

Commented:
Thanks everyone.  Here is more information:  I time races 5k, marathon, trithlons, etc) and I encode my own RFID tags.  I have to make sure that there is little or no reasonable chance of conflicting with things like rfid tags that are put into shoes, etc.  However, ultimately I have to have decimal numbers to work with.  So the two options I have landed on are these:

1) I could encode the tags using 6 decimal digits (I have never seen a six-digit decimal rfid value come through my system).  For instance, rfid tag 100001 would be used for bib #1, rfid tag 100002 would go on bib #2, etc.  Then I just loop through and subtract 100000 from each tag before processing the results.

2) I could encode the tags using hex numbers and just convert to decimal prior to processing the results.

I prefer the first method because it is human-readable but it is even more important to process quickly.

Thanks for any additional clarity you can provide.  I will look at the above links, code, suggestions, ...
IT / Software Engineering Consultant
Top Expert 2016
Commented:
I agree the first method you mention seems like the most friendly and clearer.  I think performance shouldn't be your concern, it won't take any meaningful time in either case.  Go with what makes sense logically and is the best solution from a design and usability perspective, and the tiny amount of extra processing time doesn't even come into play.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial