Decode me! (Part #2 - Winter Session)

Started by newdep, December 09, 2006, 05:48:23 PM

Previous topic - Next topic

cormullion

#15
Printing to screen - that can be quite slow, especially on my machine - all those translucent windows and Unicode fonts... :-)



To write to file just precede the code with this:


(device (open "myfile" "write"))

and then follow it with


(close (device))

You don't want your code to run too fast - your justification for going to get a cup of coffee while it's running would disappear!

newdep

#16
That coffee break is crusial in programming ;-)
-- (define? (Cornflakes))

newdep

#17
Regarindg to speed in newlisp the last few hours where quite intresting

to observe how code can be speeded up, a nice profiler would a nice

to have for newlisp ;-)



Finaly the conclusion is, when it comes to search and replace of big files

there is nothing quicker in newlist then using the straight arithmetic, in this

case modulo was the best solution!



This is changed from vigenere.lsp ->



;; push is slightly quicker then set-nth

;; (% x length code) is 50% quicker then a rotate, wrapping around 26..nice arithmatic flavor..

;; index on a list of chars is just a performance eater for nothing, removed!

;; removed array's again, normal lists are quicker! difference is nihil to 2 seconds.

;; IO read + write to file takes seconds (bus specific)

;; find is quick!



Under the line there is removed a delay of 20 seconds in total.. from my last code..

There is no time to put the water on the fire for cup of coffee  anymore...





PS; the original idea behind the code was indeed to show the working of

vigenere, performance was an issue for me until it killed my program and

i had to rewrite it as above...Le Roi est mort. Vive le Roi!



Norman.
-- (define? (Cornflakes))

cormullion

#18
speed king norman!



There are some timing comparison routines (by Fanda) - now over at Noodles, but perhaps for profiling it would be easier to have something more automatic... but I think it would have to be done at a lower level...



By the way, Lutz's (encrypt) function is pretty cool... I don't know what the method of encryption is though.

newdep

#19
Lutz uses XOR one-pad encryption, which is very save if you have

a random long key which is used only once every ;-)
-- (define? (Cornflakes))

newdep

#20
Last post...



Finaly.. this is the whole vigenere -> 3 lines of code ;-)

and 13 seconds on 5 MB text file! encode and decode.



(setq head (map char (sequence 97 122)))



(define (enc) (setq y ((rotate code -1) 0))  (head  (% (S (find x head) (find y head)) 26)))



(define (vigenere) (dolist (x text)  (if (find x head)  (push (enc) dexd -1)   (push x dexd -1)) ))
-- (define? (Cornflakes))

cormullion

#21
it's elegant, fast, and concise enough, now! :-)





But I think you should try harder:



http://cybertiggyr.com/gene/vig/vigenere.lisp">//http://cybertiggyr.com/gene/vig/vigenere.lisp



:-/

newdep

#22
you mean regarding speed or options?



regarding speed, I dont want to go quicker ;-)



My very first vigenere also has uppercase and numbers..



Perhpas ill just do the whole bunch from #0 - #127 one day ;-)



Norman.
-- (define? (Cornflakes))

Lutz

#23
Congratulations, this was a long way from 30 minutes to a few seconds. But you can still more than double the speed, if you let your algorithm work on numbers instead of exploded strings.



some small changes:


;; create the lowercase Vigenere list
(setq head (sequence 97 122))

;; ask for secret code
(setq code (map char (explode (rotate (ask "Secret code: ")))))

; reading file
...
(setq text (read-file infile))
(setq text (unpack (dup "c" (length text)) text))
...

(if (= + S) (write-file (append infile ".enc") (join (map char dexd)))
            (write-file (append infile ".dec") (join (map char dexd))))


numbers take less memory then the one-character strings and less time to manipulate.



Lutz

newdep

#24
The last Hint is from the Guru ;-)  



Lets see what it brings ;-)
-- (define? (Cornflakes))

newdep

#25
Amazing ;-)



From 13 seconds to 8 seconds ;-)



Ill make it version 1.0 ;-)



Thanks, Norman
-- (define? (Cornflakes))