3 ms·
Optimization makes a huge difference with arrays in Swift. The following took 2.37 seconds (!) on my machine un-optimized: var bytes = [UInt8](count: 16_7
by waynecochran 9y ago
Optimization makes a huge difference with arrays in Swift. The following took 2.37 seconds (!) on my machine un-optimized:
var bytes = [UInt8](count: 16_737_732, reapeatedValue: 0)
This was slow enough to block my UI (spinning pizza) -- so I ran it on a background thead!. Turn on optimization and it took a more reasonable 0.0165 seconds. BTW, took C's memset only 0.00827 seconds on the same machine.
I switched back to my beloved Objective-C for the app I was developing. Won't be using Swift for low-level image processing.
- bluedino 9y ago>> I switched back to my beloved Objective-C for the app I was developing. Can you write the app in Swift (if there are advantages to doing so) and then write the image processing routines/library in C?
- waynecochran 9y agoYes, but I hate figuring out the bridging stuff when I can just use C directly in Objective-C.
- adamnemecek 9y agoThe bridging is pretty straight forward no? You add a bridging header and put in all the headers you want to expose to swift and you are good to go.
- waynecochran 9y agoPretty straight forward, but there is zero friction when using Obj-C.
- tentaTherapist 9y agoYou say 'beloved Objective-C'. Sorry if this is really off topic, but is objc_msgsend (the whole late-binding thing) or anything else about Objective-C particularly appealing? Maybe the decent unoptimized speeds or the fact that you can use plain C whenever?
- waynecochran 9y agoI love C because I do a lot of low-level programming and I feel closer to the machine with C. For higher level UI stuff I like the Smalltalk extensions to C and Obj-C's dynamic nature and the fact that I can use C (and C++ directly). Also I have been programming the Mac for a long time and I have a lot of mental investment in Objective-C. OTOH, I do like the functional, strongly-type features of Swift, but like any higher level language I feel more removed from the machine. That's the price for abstraction.
- auggierose 9y agoI found that when doing lots of array code that needs to be fast, you cannot rely on the high-level abstractions of normal Swift. But even while staying in Swift there are API's to drop down to, lower levels of array manipulation etc. where you get the speed of raw C (independent of your optimisation settings).
- waynecochran 9y agoYeah, but the amount of code and API's for C make using Swift a bit of a pain when using the two.
- cageface 9y agoI prefer Swift for general purpose iOS development but I have one app that leans heavily on a lot of C++ DSP code and that one is likely to stay ObjC just because interfacing ObjC & C++ is so painless.
- Razengan 9y agoYou could mix Objective-C and Swift.
- waynecochran 9y agoYou can, but not as easily.
- pjmlp 9y agoWhat about using unsafe Array access if you want C style code?