Loading large two-dimensional arrays in Swift 3.0.

Asked 2 years ago, Updated 2 years ago, 59 views

Swift can load a large two-dimensional array, for example code.

let p:[[Int]] = [[10,6],[42,42],[30,46],[42,36],[7,9],[22,24],[29,25],[16,1],[15,38],[35,7],[34,14],[14,4],[34,6],[9,15],[13,31],[11,46],[40,42],[16,13],[2,18],[28,16],[22,9],[32,42],[43,34],[22,26],[16,9],[43,18],[47,3],[11,11],[49,23],[3,22],[12,43],[49,42],[49,38],[33,24],[48,46],[17,25],[29,18],[6,38],[26,29],[43,8],[8,5],[17,3],[24,39],[38,9],[33,35],[2,15],[28,46],[32,11],[20,43],[8,35]]

If the size of the array is a little longer, it takes too long to compile. In code like Python or Ruby, that size of an array can be initialized and used immediately, but Swift doesn't.

Is there an easy way to initialize a set value in a large array?

I'm considering putting the value in the file and then parsing it to create an array, but I'd like to put it right into the code if possible.

swift codesquad

2022-09-21 18:24

2 Answers

I understand that there is an issue with the indexing problem in swift.

If you keep indexing, you will have a problem that the build cannot be made. So the solution that came out through googling was to initialize the value through append(_:

var p: [[Int]] = []
p.append([10,6])
p.append([42,42])
....

If you initialize the value in this way, you have a readability problem, and of course the code is longer.

차라리 파일이나 json 타입으로 가지고 와서 루프 문을 이용해서 배열을 만들어 쓰시는게 낫지 않을까 싶습니다.


2022-09-21 18:24

Array and dictionary literal compilation issues have been bugged in Swift 2.x

It's an issue where it takes 12 hours to google it https://bugs.swift.org/browse/SR-305?jql=text%20~%20%22long%20array%22

It's been partially improved in Swift 3, so it's still slow.

What I've tested and checked is inside the array tunnel Up to 128 will operate as O(1) After that, it gradually slows down to O(n).

The Swift Compiler is structurally less optimized for performance Swift 4 expects to improve further.

Added.

In another sense, if you look at it, the code that declares more than 128 lines is good I don't think so :)


2022-09-21 18:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.