[swift-users] Compile time exceeded. Anything wrong?

www.hbu.cn at 163.com www.hbu.cn at 163.com
Mon Jun 5 21:28:50 CDT 2017


thanks,  guys!  dp array should be var, not let, but the compile run too
slow. It indeed a bug.  Optimizing  the code for the compiling time is
really a headache for coders.  I am using the Xcode 8.1. Hoping it can be
fixed in next release.


*best wishes for you *

2017-06-06 8:57 GMT+08:00 Geordie J <geojay at gmail.com>:

>
> Am 06.06.2017 um 09:02 schrieb Jens Persson via swift-users <
> swift-users at swift.org>:
>
> When compiling that from the command line, I get the following (after
> about 6 seconds):
>
> test.swift:7:18: error: cannot assign through subscript: 'dp' is a 'let'
> constant
>         dp[0][0] = 0
>         ~~       ^
> /.../
>
> After fixing that (changing let dp to var dp), it will compile
> successfully, still taking a very long time though. This usually means that
> some expression(s) in the code happen to be a bit hard on the type checker
> (in its current state).
> I tried the latest dev snapshot and it is a bit faster, perhaps 3 s
> instead of 6.
>
> Anyway, here is a logically equivalent rewrite which will compile faster:
>
> class Solution {
>     func rob(nums: [Int]) -> Int {
>         guard nums.count > 0 else { return 0 }
>         var dp = Array.init(repeating: Array.init(repeating: 0, count:
> nums.count),
>                             count: 2)
>         dp[0][0] = 0
>         dp[0][1] = nums[0]
>         for i in 1 ..< nums.count {
>           let dp_iMinus1_0 = dp[i - 1][0]
>           let dp_iMinus1_1 = dp[i - 1][1]
>             dp[i][0] = max(dp_iMinus1_0, dp_iMinus1_1)
>             dp[i][1] = dp_iMinus1_0 + nums[i]
>
>
> Just a nitpick: this isn’t functionally equivalent to the original code
> (dp[i][1] = dp[i][0] + nums[i]), because dp[i][0] might have actually
> changed on the previous line. But you can return this line to the original
> version without any effect on the compile time (less than 250ms on my
> machine).
>
> I think the compile time issue is that max() is a generic function that
> takes any Comparable, so the type checker seems to go berserk trying to
> ensure the term is satisfiable.
>
> If you create a non-generic replacement for max in the same file:
>
> private func myMax(_ x1: Int, _ x2: Int) -> Int {
>     return (x1 >= x2) ? x1 : x2
> }
>
> … and replace the assignment to *max(dp[i - 1][0], ……)* in the for loop
> with *myMax(dp[i - 1][0], ….)*
>
> The compile time will be just as fast as the one that swaps out the
> internal elements (on my machine it’s actually about 10% faster with the
> non-generic *max*).
>
> Regards,
> Geordie
>
>         }
>         return 0
>     }
> }
>
>
>
>
>
> On Mon, Jun 5, 2017 at 2:32 PM, Hbucius Smith via swift-users <
> swift-users at swift.org> wrote:
>
>> Hi Swift-Users,
>>
>>     when I compiled the code, Xcode cannot stop, I do not know why. It is
>> very strange. Can anyone help ? Here is the code. I am using Xcode 8.1
>>
>> class Solution {
>>
>>     func rob(nums: [Int]) -> Int {
>>
>>         guard nums.count > 0 else { return 0 }
>>
>>         let dp = Array.init(repeating: Array.init(repeating: 0, count:
>> nums.count),
>>
>>                             count: 2)
>>
>>         dp[0][0] = 0
>>
>>         dp[0][1] = nums[0]
>>
>>         for i in 1 ..< nums.count {
>>
>>             dp[i][0] = max(dp[i - 1][0], dp[i - 1][1])
>>
>>             dp[i][1] = dp[i - 1][0] + nums[i]
>>
>>         }
>>
>>         return 0
>>
>>     }
>>
>> }
>>
>>
>>
>> *best wishes for you *
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170606/d5021ce4/attachment.html>


More information about the swift-users mailing list