Forum > Third party

Alternative set of string-to-int conversion routines

(1/16) > >>

avk:
Hi!
For a long time I was going to try to create a string-to-integer conversion function that would be noticeably faster than the built-in Val().
There seems to be something like this at the moment. Anyway, a quick and dirty benchmark against Val() from the current development version of FPC on my machine looks like this:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Int32:Val(), score:        2853TryChars2Int, score: 1137Int64:Val(), score:        3045TryChars2Int, score: 1121 Parsing is carried out basically according to the same rules as in the built-in Val() (well, as far as I understood them). IMO there is only one significant difference from Val(): since the lone leading zero is the prefix of an octal string, decimal strings cannot start with a zero.
The set also includes functions that accept an array of char and a PChar.
Feedback and comments are highly appreciated, if any.

Thaddy:
Nice. One remark: there is a lot of duplicate code and that can be avoided.
There are two options I can see:
1. You could write generic functions, implemented and declared in the implementation section. That will (should) not affect speed since generics are templates.
2. Use the technique sometimes used in the rtl, where a header include uses the same implementation include per routine wherein the type is changed in the header, so the implementation include knows the correct type. (predates generics) Also should not affect speed.

But compliments. I find the same speed gain, percent wise.

zamtmn:
Please add

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TryStr2xxx(const s: string; const aIndex, aCount: SizeInt; out aValue: xxx): Boolean; inline;

avk:
@Thaddy, thank you.
You're right, a lot of code is duplicated and I don't have an option yet to avoid this without sacrificing performance. On the other hand, this set is still a proof of concept.

@zamtmn, doesn't this code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if TryChars2Int(s[5..15], MyInt) then    ... cover your case?

zamtmn:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TryStrToInt(Copy(s,5,10),MyInt)also covers my case, but the point is to make it fast, without unnecessary allocations and copies

Navigation

[0] Message Index

[#] Next page

Go to full version