Welcome to the CHICKEN Scheme pasting service

string-replace* added by mario-goulart on Fri Apr 6 05:59:51 2012

;;;;;;;; python ;;;;;;;;

$ cat sreplace.py
#! /usr/bin/env python

iters = 3000000

s = "abcdefghijklmnopqrstuv"
a = "u"
b = "---"


while iters > 0:
    s.replace(a, b)
    iters = iters - 1

print s.replace(a, b)


;;;;;;;; chicken ;;;;;;;;


$ cat sreplace.scm
(define (string-replace* str orig replacement)
  (let ((str-len (string-length str))
        (orig-len (string-length orig)))
    (let loop ((pos 0))
      (if (eq? pos str-len)
          str
          (let ((char (string-ref str pos)))
            (if (eq? char (string-ref orig 0))
                (if (##sys#compare-bytes str orig pos 0 orig-len)
                    (##sys#string-append
                     (##sys#string-append
                      (##sys#substring str 0 pos)
                      replacement)
                     (##sys#substring str (fx+ pos orig-len) str-len))
                    (loop (fx+ 1 pos)))
                (loop (fx+ 1 pos))))))))

(define iter 3000000)

(define s "abcdefghijklmnopqrstuv")
(define a "u")
(define b "---")

(let loop ((i iter))
  (unless (fx= i 0)
    (string-replace* s a b)
    (loop (fx- i 1))))

(print (string-replace* s a b))



;;;;;;;; benchmarks ;;;;;;;;


$ time ./sreplace.py
abcdefghijklmnopqrst---v

real    0m1.885s
user    0m1.876s
sys     0m0.008s


$ csc -O5 sreplace.scm && time ./sreplace
abcdefghijklmnopqrst---v

real    0m2.172s
user    0m2.144s
sys     0m0.016s

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which procedure can be used to check whether its argument is a procedure?
Visually impaired? Let me spell it for you (wav file) download WAV