[swift-users] Parsing binary data
Dmitri Gribenko
gribozavr at gmail.com
Fri Jul 8 02:36:46 CDT 2016
On Fri, Jul 8, 2016 at 12:15 AM, Tino Heth via swift-users
<swift-users at swift.org> wrote:
> Hi there!
>
> Some weeks ago I wrote a parser for a binary format, but it's performance was disastrous, and I knew how to easily outperform this first approach with Objective-C by large.
> Now, I'm about to write a different parser, which of course ;-), I'd prefer to code in Swift.
> Working with raw bytes most likely won't ever be a thing where Swift shines, but I guess there are ways to do so without compromising speed… so, are there any established best-practices and things to avoid? Or is it less hassle to go back to C for low-level stuff?
It should be simple: use Array for your data. Avoid creating
intermediate arrays that hold copies of parts of your data when
possible. It is OK to slice the array and create ArraySlices. This
should give you speed close to C.
If you are not satisfied with performance, feel free to post your code
here with some commentary, and someone might look at it and see if
there is any performance advice we could give you.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-users
mailing list