Improvements to the Swift implementation #389
odmir
started this conversation in
Improvement suggestions
Replies: 2 comments 9 replies
-
As I understand it (just as someone watching this)
|
Beta Was this translation helpful? Give feedback.
9 replies
-
Thank you @rbergen for moving this to the right place, I hadn't noticed the discussions section. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! I am looking at the current Swift implementation of the sieve and have some changes in mind that in my machine lead to ~10x speed improvement. The thing is that I consider these changes to be relatively small, and since they don't change the algorithm in any way, I consider them unworthy of a separate solution.
The changes I'm considering are:
Bit count
flag in the README to 8 bits since the implementation is using an array of booleans that each occupy 8 bits in memory, even if only one of those bits is used to represent the value (currently it's set tounknown
)PrimeSwift/solution_1/Sources/PrimeSieveSwift/main.swift
:BitArray
protocol definition (lines 3-7) that is unnecessarily being used as an existential and replace its use in line 33 by the concretestruct BoolBitArray
that is already implementedPrimeSieveSwift
from a class to a struct (at line 31) and make therunSieve
methodmutating
(line 52) to indicate that the method mutates the structbits=8
tag to the program output at line 117Then there are more "stylistic" changes that make the code more idiomatic:
sieve
variable to an empty sieve instead (var sieve: PrimeSieveSwift!
->var sieve = PrimeSieveSwift(limit: 0)
)idx % 2 != 0
by the more descriptive and idiomatic!idx.isMultiple(of: 2)
PrimeSieveSwift
type toPrimeSieve
bits
property to the more descriptive name,isPrime
BoolBitArray
to be a pair of subscript getter and setter, keeping the indexing implementation the same, and changing every use of thebits
property to use the subscript operator likebits[num]
andbits[num] = false
(at lines 45, 58, 64 and 103), instead of the currentgetBit
andclearBit
My first question is if I should contribute this as a separate new solution or to the existing one.
The contribution guidelines say this under faithfulness:
So my next question is if my change from a class to a struct falls under "equivalent feature". A C++ class is closer to a Swift struct than a Swift class, so I guess there is no issue here.
Thank you for your attention.
Beta Was this translation helpful? Give feedback.
All reactions