How can we can get at least close to the speed of that? pasted by mario-goulart on Tue Apr 3 23:07:03 2012
def string_replace(str, replacements): for replacement in replacements: str = str.replace(replacement[0], replacement[1], 1) return str s = "JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format." rpls = [["so", "--"], ["http", "xxxx"], ["abc", "---"], ["json", "xxxx"]] i = 10000 a = '' while i > 0: a = string_replace(s, rpls) i = i - 1 print a
Scheme version for the python code (replacing strings) pasted by mario-goulart on Wed Apr 4 16:27:24 2012
(use data-structures) (define iters 10000) (define s "JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.") (define rpls '(("so" . "--") ("http" . "xxxx") ("abc" . "---") ("json" . "xxxx"))) (define (bench-string-translate) (let loop ((i iters)) (unless (fx= i 0) (string-translate* s rpls) (loop (fx- i 1))))) (print (string-translate* s rpls)) (time (bench-string-translate))
replacing with core procedures pasted by sjamaan on Wed Apr 4 19:50:33 2012
(use data-structures) (define iters 10000) (define s "JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.") (define rpls '(("so" . "--") ("http" . "xxxx") ("abc" . "---") ("json" . "xxxx"))) (define (replace subject target replacement) (let ((sl (string-length subject)) (rl (string-length replacement)) (i (substring-index subject target))) (if (not i) subject (string-append (substring subject 0 i) replacement (substring s (+ i rl) (+ sl 1)))))) (define (my-replace str replacements) (foldl (lambda (s r) (replace s (car r) (cdr r))) str replacements)) (define (bench-string-translate) (let loop ((i iters)) (unless (fx= i 0) (my-replace s rpls) (loop (fx- i 1))))) (print (my-replace s rpls)) (time (bench-string-translate))
simpler. shorter. faster: with srfi-13 pasted by sjamaan on Wed Apr 4 19:59:56 2012
(use srfi-13 data-structures) (define iters 10000) (define s "JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.") (define rpls '(("so" . "--") ("http" . "xxxx") ("abc" . "---") ("json" . "xxxx"))) (define (my-replace str replacements) (foldl (lambda (s r) (let ((i (substring-index s (car r)))) (if i (string-replace s (cdr r) i (string-length (car r))) s))) str replacements)) (define (bench-string-translate) (let loop ((i iters)) (unless (fx= i 0) (my-replace s rpls) (loop (fx- i 1))))) (print (my-replace s rpls)) (time (bench-string-translate))
fixed replace added by mario-goulart on Wed Apr 4 22:57:54 2012
(use data-structures) (define iters 100000) (define s "JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.") (define rpls '(("so" . "--") ("http" . "xxxx") ("abc" . "---") ("json" . "xxxx"))) (define (replace subject target replacement) (let ((i (substring-index target subject))) (if i (string-append (substring subject 0 i) replacement (substring subject (+ i (string-length target)))) subject))) (define (my-replace str replacements) (foldl (lambda (s r) (replace s (car r) (cdr r))) str replacements)) (define (bench-string-translate) (let loop ((i iters)) (unless (fx= i 0) (my-replace s rpls) (loop (fx- i 1))))) (print (my-replace s rpls)) (time (bench-string-translate))