On Sunday, January 17, 2016 at 10:09:36 AM UTC-5, Jon Zeppieri wrote: > Oops: that final else was wrong. If all we encounter in the string is > whitespace, the result is the empty string, not the input string, so: > > > ;; === > > (require racket/unsafe/ops) > > > (define (string-trim s) > (define len (string-length s)) > > (let loop ([i 0]) > (cond [(unsafe-fx< i len) > (cond [(char-whitespace? (unsafe-string-ref s i)) > (loop (unsafe-fx+ i 1))] > [else > (let inner ([j (unsafe-fx- len 1)]) > (cond [(char-whitespace? (unsafe-string-ref s j)) > (inner (unsafe-fx- j 1))] > [else > (substring s i (unsafe-fx+ j 1))]))])] > [else > ""]))) > ;; === > > > > > > > > On Sun, Jan 17, 2016 at 1:24 AM, Jon Zeppieri <[email protected]> wrote: > > > > > > On Sat, Jan 16, 2016 at 11:29 PM, Brian Adkins <[email protected]> wrote: > > > I'm happy to run experiments and report timings though. > > > > > > > > Since the profile suggests that string-trim is the biggest culprit (followed > by fprintf), try using this specialized version of string-trim locally: > > > ;; === > > (require racket/unsafe/ops) > > > (define (string-trim s) > (define len (string-length s)) > > (let loop ([i 0]) > (cond [(unsafe-fx< i len) > (cond [(char-whitespace? (unsafe-string-ref s i)) > (loop (unsafe-fx+ i 1))] > [else > (let inner ([j (unsafe-fx- len 1)]) > (cond [(char-whitespace? (unsafe-string-ref s j)) > (inner (unsafe-fx- j 1))] > [else > (substring s i (unsafe-fx+ j 1))]))])] > [else > s]))) > ;; === > > > Instead of fprintf-ing the tabbed values, you might try (displayln > (string-join fields "\t")). Of course, that requires building a list of > strings, which has its own cost.
With built-in string-trim, the lowest of three runs was 10293. Using your string-trim the lowest of three runs was 7618, so it reduced the runtime by 26%. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

