Sometimes REGEX are too slow for simple things that could be better done with the
Do you think it could be possible to have a
Thanks
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
#!/usr/bin/newlisp -n -c
(context 'pool)
(define (pool:pool task (poolSize 5) (syncDelay 50))
(while (> (length (sync)) poolSize)
(sync syncDelay))
(spawn 's (eval task)))
(define (pool:map task liste (poolSize 5) (syncDelay 50))
(let (size (- (length liste) 1)
out '())
; spawn processes
(for (i 0 size)
; Wait for availlable "slot" in pool
(while (>= (length (sync)) poolSize)
(sync syncDelay))
(spawn (sym i) (task (liste i))))
(sync 10000)
; get back values
(for (i 0 size)
(push (eval (sym i)) out -1))))
(context 'MAIN)
(define (test-func x)
(sleep (+ 5 (rand 100)))
(* x x))
(println
"Map without pooling : "
(time (map test-func (sequence 1 100)))
"ms")
(println
"Pool map, default pool size : "
(time (pool:map test-func (sequence 1 100)))
"ms")
(println
"Pool:map, pool size = 50 : "
(time (pool:map test-func (sequence 1 100) 50))
"ms")
(println
"Just to check results : "
(pool:map test-func (sequence 1 100) 50))
(println
"Pool:map on string : "
(join (pool:map (lambda (c) (upper-case c)) "A beautiful string") "") )
(exit)
#!/usr/bin/newlisp
(set 'nThreads 503
'limit 500000
'channels '())
; The worker code
(define (worker id chanR chanW , r)
(while (< r limit)
(write-line chanW (string (+ 1 (set 'r (int (read-line chanR)))))))
(when (= r limit) (println "Done by id:" id))
)
; make communication channels
(dotimes (i nThreads) (push (pipe) channels))
; spawn workers
(dotimes (i nThreads)
(let ( id (+ i 1)
in (channels i 0)
out (channels (% (+ 1 i) nThreads) 1) )
(spawn 'p (worker id in out))))
(set 'start (time-of-day))
; put the token in the ring
(write-line ((channels 0) 1) "0")
; wait for threads end
(until (sync 10))
(println "Duration: " (- (time-of-day) start) "ms")
(exit)
(set 'nThreads 503
'limit 500000
'channels '())
; worker do that
(define (worker id chanR chanW , r)
(catch
(while (set 'r (int (read-line chanR)))
(if (= r limit)
(throw id)
(write-line chanW (string (inc r)))))
)
)
; make communication channels
(for (i 0 nThreads) (push (pipe) channels -1))
; spawn workers
(for (i 0 nThreads)
(spawn 'p (worker (+ i 1)
((channels i) 0)
((channels (% (+ 1 i) nThreads)) 1) )) )
; start the process by giving the token to first thread
(write-line ((channels 0) 1) "0")
; wait for result
(while (not p) (sleep 100) (sync 1))
(println "DONE BY " p)
(abort)
(exit)
(define (heads l)
(unique (map first l))
)
(define (members l class)
(filter (fn (x) (= class (first x))) l)
)
(define (doit l)
(map (fn (c) (members l c)) (heads l))
)
(define-macro (with-events _todo_)
(let (_fun_ (string (_todo_ 0)) _res_ nil)
(eval (EVENTS-BEFORE _fun_))
(setf _res_ (eval _todo_))
(eval (EVENTS-AFTER _fun_))
_res_
)
)
> (event-before + (setf (_todo_ 2) 100))
> (with-events (+ 4 5 6))
110
> (event-after + (println "args : " (rest _todo_)))
> (with-events (+ 4 5 6))
args : (4 100 6)
110
(new Tree 'EVENTS-BEFORE)
(new Tree 'EVENTS-AFTER)
(define-macro (event-before target action)
(setf target (string target))
(unless (EVENTS-BEFORE target) (EVENTS-BEFORE target '(begin)) )
(extend (EVENTS-BEFORE target) (list action))
)
(define-macro (event-after target action)
(setf target (string target))
(unless (EVENTS-AFTER target) (EVENTS-AFTER target '(begin)) )
(extend (EVENTS-AFTER target) (list action))
)
(define-macro (with-events)
(let (_fun_ (string (args 0)) _res_ nil)
(eval (EVENTS-BEFORE _fun_))
(setf _res_ (eval $args))
(eval (EVENTS-AFTER _fun_))
_res_
)
)
;; for now, same as (+ 4 5 6)
> (with-events + 4 5 6)
15
;; add a new "before" add event
> (event-before + (println "will add something : " $args))
;; then an "after" event
> (event-after + (println "done!"))
;; then, if i call "+" with events :
> (with-events + 4 5 6)
will add something : (+ 4 5 6)
done!
15
;; in my framework :
(define (output)
(with-events output-headers)
(with-events output-content)
)
;; in a session handling extension
(event-before output-headers (SESSION:save))
;; in a text formater extension
(event-before output-content (TYPOGRAPHIST:reformat-all))
;; somwhere else
(event-after output-content (LOGGER:do-something))