News for hackers. Stuff that matters.
Re:Building Web Apps in Go by fauigerzigerk on Tuesday March 29, 2016 @05:14AM (#11380341)
I tried to avoid bridging as far as possible and I'm working directly with UTF-16 because that's the fastest view by far. I'm using the latest release version of Xcode on a Mac.

The simplest benchmark I used is a function that does the equivalent of

  str.split(/\s*,\s*/)
So it splits a string around a separator and removes any whitespace around the items. For a million strings the numbers I get are as follows:

Swift: 3.5 seconds

Java: 0.31 seconds (after warmup)

So that makes Java more than 10 times as fast as Swift.

Here's the source code. I would be more than happy if you could tell me that I'm doing something horribly wrong (which I frequently do):

    import Foundation

    let N = 1000000

    func generateTestData() -> [String] {
        var a = [String]()
        for _ in 0..<N {
            a.append(",,abc, 123  ,x, , more more more,\u{A0}and yet more, ")
        }
        return a
    }

    func splitAndTrim(s: String, sep: UInt16) -> [String] {
        var result = [String]()
        result.reserveCapacity(10)
        
        let space = NSCharacterSet.whitespaceAndNewlineCharacterSet()
        let cs = s.utf16
        let eos = cs.endIndex
        var begin = eos
        var end = eos
        
        for i in cs.startIndex..<eos {
            let c = cs[i]
            if c == sep {
                result.append(begin == eos ? "" : String(cs[begin..<end.successor()]))
                begin = eos
                end = eos
            } else if !space.characterIsMember(c) {
                if begin == eos {
                    begin = i
                }
                end = i
            }
        }
        
        result.append(begin == eos ? "" : String(cs[begin..<end.successor()]))
        return result
    }

    func doSplits(data: [String]) -> Int {
        var count = 0
        let sep = ",".utf16.first!
        for s in data {
            let parts = splitAndTrim(s, sep: sep)
            count += parts.count
        }
        return count
    }

    let data = generateTestData()
    let start = NSDate()
    let sum = doSplits(data)
    print("elapsed: \(NSDate().timeIntervalSinceDate(start))")
    print("sum: \(sum)")
And here's the Java code:

    import java.util.List;
    import java.util.ArrayList;

    public class ParseTest {
    
        static int N = 1000000;
    
        public static void main(String[] args) {
            List<String> data = generateTestData();
            for (int i = 0; i < 3; ++i) {
                long start = System.currentTimeMillis();
                int sum = doSplits(data);
                System.out.println("elapsed: " +
                        ((System.currentTimeMillis() - start) / 1000.0 + " seconds"));
                System.out.println("sum: " + sum);
            }
        }
    
        static int doSplits(List<String> a) {
            int count = 0;
            for (String s : a) {
                List<String> parts = splitAndTrim(s, ',');
                count += parts.size();
            }
            return count;
        }
    
        static List<String> splitAndTrim(String s, char sep) {
            List<String> result = new ArrayList<>(10);
            int eos = s.length();
            int begin = eos;
            int end = eos;
    
            for (int i = 0; i != eos; ++i) {
                char c = s.charAt(i);
                if (c == sep) {
                    result.add(begin == eos ? "" : s.substring(begin, end + 1));
                    begin = eos;
                    end = eos;
                } else if (!Character.isSpaceChar(c)) {
                    if (begin == eos) {
                        begin = i;
                    }
                    end = i;
                }
            }
    
            result.add(begin == eos ? "" : s.substring(begin, end + 1));
            return result;
        }
    
    
        static List<String> generateTestData() {
            List<String> a = new ArrayList<>();
            for (int i = 0; i < N; i++) {
                a.add(",,abc, 123  ,x, , more more more,\u00A0and yet more, ");
            }
            return a;
        }
    
    }

There are no comments on this comment yet.