<div dir="ltr">My mistake. If I create a new array I get the problem. EG:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">import Foundation<br><br>func elapsed(s: DispatchTime, e: DispatchTime) -> Double {<br> return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br>}<br><br>let s = 250_000_000<br>var a = [UInt8]()<br>a.reserveCapacity(s)<br><br>let sa = DispatchTime.now()<br>for i in 1 ... s {<br> a.append(0)<br>}<br>let ea = DispatchTime.now()<br>print("Append time: \(elapsed(s: sa, e: ea)) s")<br><br>let st = DispatchTime.now()<br>a = Array(a[0 ..< (s >> 1)])<br>let et = DispatchTime.now()<br>print("Trim time: \(elapsed(s: st, e: et)) s")<br>print("a count: \(a.count), capacity: \(a.capacity)")<br><br><br></blockquote>Prints:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">Append time: 2.65726525 s<br>Trim time: 36.954417356 s<br>a count: 125000000, capacity: 125001696</blockquote></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 9 May 2017 at 17:53, Howard Lovatt <span dir="ltr"><<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I find trimming relative to appending OK on my 6 year old MacBook Pro. EG:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">import Foundation<br><br>func elapsed(s: DispatchTime, e: DispatchTime) -> Double {<br> return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br>}<br><br>let s = 250_000_000<br>var a = [UInt8]()<br>a.reserveCapacity(s)<br><br>let sa = DispatchTime.now()<br>for i in 1 ... s {<br> a.append(0)<br>}<br>let ea = DispatchTime.now()<br>print("Append time: \(elapsed(s: sa, e: ea)) s")<br><br>let st = DispatchTime.now()<br>let ha = a[0 ..< (s >> 1)]<br>let et = DispatchTime.now()<br>print("Trim time: \(elapsed(s: st, e: et)) s")<br><br>print("ha count: \(ha.count), capacity: \(ha.capacity)")<br><br><br></blockquote>Prints:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">Append time: 2.612397389 s<br>Trim time: 0.000444132 s<br>ha count: 125000000, capacity: 125000000</blockquote></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><div class="m_-6131575969107924050gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div></font></span><div><div class="h5">
<br><div class="gmail_quote">On 9 May 2017 at 12:56, Kelvin Ma via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Depending on what you’re trying to do with the data, you might be better off using an UnsafeBufferPointer and allocating and reallocating that, C-style.<br></div><div class="m_-6131575969107924050HOEnZb"><div class="m_-6131575969107924050h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 8, 2017 at 7:01 PM, Philippe Hausler via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I wonder if Data might be a better tool for the job here since it is it’s own slice type and would avoid copying large amounts of data into temporary buffers.<br>
<div class="m_-6131575969107924050m_7331902355709494831HOEnZb"><div class="m_-6131575969107924050m_7331902355709494831h5"><br>
> On May 8, 2017, at 16:47, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> I have this C library that interacts with some hardware over the network that produces a ton of data. It tells me up front the maximum size the data might be so I can allocate a buffer for it, then does a bunch of network requests downloading that data into the buffer, then tells me when it's done and what the final, smaller size is.<br>
><br>
> Thanks to previous discussions on the list, I settled on using a [UInt8] as the buffer, because it let me avoid various .withUnsafePointer{} calls (I need the unsafe buffer pointer to live outside the scope of the closures). Unfortunately, When I go to shrink the buffer to its final size with:<br>
><br>
> self.dataBuffer = Array(self.dataBuffer![0 ..< finalBufferSize])<br>
><br>
> This ends up taking over 2 minutes to complete (on an iPad Pro). finalBufferSize is very large, 240 MB, but I think it's doing a very naive copy.<br>
><br>
> I've since worked around this problem, but is there any way to improve on this?<br>
><br>
> Thanks,<br>
><br>
> --<br>
> Rick Mann<br>
> <a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br></div>