Common Lisp練習 - CodeChef : TSORT
October 24, 2012 at 06:00 PM | categories: programming, common lisp |Common Lispの練習にCodeChefの↓の練習問題をやってみた。
http://www.codechef.com/problems/TSORT
問題自体は全然難しくないけど、Common Lispで解こうとしたらTime Limit Exceededでおちてしまった。
最初は以下のように書いて、
(let ((_n (parse-integer (read-line))) (lis ())) (dotimes (i _n) (push (parse-integer (read-line)) lis)) (setf lis (sort lis #'(lambda (x y) (< x y)))) (dolist (x lis) (format t "~a~%" x)))
以下の用にして時間を測ったところ
$ time ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' | sbcl --script turbosort.cl > /dev/null ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' 1.19s user 0.01s system 91% cpu 1.311 total sbcl --script turbosort.cl > /dev/null 3.42s user 0.43s system 97% cpu 3.938 total
ローカルだと3.42s程度だった。codechef上での制限は5secなのでセーフかと思ったけどTime Limit Exceededだった。
そこで、vectorを使うように改良
(let* ((_n (parse-integer (read-line))) (lis (make-array _n :fill-pointer 0))) (dotimes (i _n) (vector-push (parse-integer (read-line)) lis)) (setf lis (sort lis #'(lambda (x y) (< x y)))) (loop for i across lis do (format t "~a~%" i)))
$ time ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' | sbcl --script turbosort.cl > /dev/null ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' 1.21s user 0.01s system 94% cpu 1.289 total sbcl --script turbosort.cl > /dev/null 2.64s user 0.44s system 98% cpu 3.137 total
若干改善されたが、まだTime Limit Exceededだった。
read-sequenceで読み込んだほうが早いかと思って以下のように書いてみた。
(defun parse-input (str) (loop for i = 0 then (+ 1 j) as j = (position #\Newline str :start i) as k = (parse-integer (subseq str i j) :junk-allowed t) if (not (null k)) collect k while j)) (let* ((_n (parse-integer (read-line))) (lis (make-array (* _n 20) :element-type 'character)) (nums ()) ) (read-sequence lis *standard-input*) (setf nums (sort (parse-input lis) #'(lambda (x y) (< x y)))) (loop for i in nums do (format t "~a~%" i)))
$ time ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' | sbcl --script turbosort.cl > /dev/null ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' 1.13s user 0.01s system 97% cpu 1.159 total sbcl --script turbosort.cl > /dev/null 3.67s user 0.48s system 96% cpu 4.297 total
残念ながら逆に遅くなってしまった。parse-inputの部分で60%くらい時間がかかっていた。
あと、#'(lambda (x y) (< x y))
の部分を #'<
にするとなぜか遅くなる。
まだ誰もlispではパスしてない模様。こういうのをもっと高速に書く方法あるのだろうか。
ちなみにCだと余裕。ローカルだと0.2秒くらいだけどリモートでは3秒くらいかかってた。そもそもCodeChefの実行環境がしょぼすぎる疑惑が。。
#include <stdio.h> #include <stdlib.h> int f(const void *i, const void *j) { return *((int*)i) > *((int*)j); } int main(void) { char buf[256]; int num = atoi(fgets(buf, 256, stdin)); int lis[num]; for (int i = 0; i < num; ++i) { lis[i] = atoi(fgets(buf, 256, stdin)); } qsort(lis, num, sizeof(int), f); for (int i = 0; i < num; ++i) { printf("%d\n", lis[i]); }
$ time ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' | ./a.out > /dev/null ruby -e 'n=1000000;puts n; n.times{puts (rand * 10000000).to_i}' 0.99s user 0.01s system 99% cpu 1.006 total ./a.out > /dev/null 0.18s user 0.01s system 17% cpu 1.116 total
blog comments powered by Disqus
About Me
mojavy |
Recent posts
95/5 Mbps とは
(August 30, 2015 at 04:22 PM)組み込み用プログラミング言語のパフォーマンス比較
(April 21, 2015 at 01:10 AM)最近読んだ本
(April 05, 2015 at 01:23 PM)Phabricatorを使ったワークフローについて
(March 02, 2015 at 08:55 PM)dnsimpleでダイナミックDNSをつかう
(December 23, 2014 at 08:02 PM)www2014のアドテク関連のResearch Trackメモ
(October 06, 2014 at 09:05 PM)flappymacs がMELPAに登録されました
(July 16, 2014 at 01:07 AM)EmacsでFlappy Birdっぽいもの書きました
(July 10, 2014 at 08:01 PM)
Recent Popular posts
Popular posts
Categories
- C (rss) (3)
- R (rss) (1)
- adtech (rss) (1)
- advent calendar (rss) (2)
- algorithms (rss) (2)
- android (rss) (2)
- aws (rss) (1)
- blog (rss) (2)
- blogofile (rss) (3)
- books (rss) (1)
- c++ (rss) (1)
- chef (rss) (4)
- common lisp (rss) (10)
- debian (rss) (2)
- dns (rss) (1)
- elasticsearch (rss) (1)
- elf (rss) (1)
- elisp (rss) (1)
- emacs (rss) (5)
- english (rss) (1)
- game (rss) (2)
- gearman (rss) (1)
- git (rss) (1)
- github (rss) (1)
- gitlab (rss) (1)
- golang (rss) (2)
- history (rss) (1)
- impress.js (rss) (1)
- internet (rss) (1)
- ios (rss) (3)
- jekyll (rss) (1)
- jenkins (rss) (1)
- linux (rss) (4)
- lisp (rss) (2)
- ltsv (rss) (1)
- lua (rss) (1)
- mac (rss) (3)
- mach-o (rss) (1)
- memo (rss) (2)
- mustache (rss) (1)
- note (rss) (1)
- objective-c (rss) (4)
- os (rss) (1)
- osx (rss) (2)
- others (rss) (1)
- paco (rss) (1)
- pdf (rss) (1)
- php (rss) (2)
- postfix (rss) (1)
- programming (rss) (12)
- project management (rss) (1)
- python (rss) (5)
- quicklinks (rss) (6)
- raspberry pi (rss) (2)
- redmine (rss) (1)
- reveal.js (rss) (1)
- ruby (rss) (10)
- sbcl (rss) (2)
- security (rss) (1)
- shell (rss) (2)
- smtp (rss) (1)
- solr (rss) (1)
- statistics (rss) (2)
- tips (rss) (10)
- tmux (rss) (3)
- toml (rss) (1)
- tools (rss) (1)
- twitter (rss) (1)
- ubuntu (rss) (1)
- unix (rss) (5)
- v8 (rss) (1)
- web (rss) (7)
- xcode (rss) (1)
- zeromq (rss) (2)
Archives
- August 2015 (1)
- April 2015 (2)
- March 2015 (1)
- December 2014 (1)
- October 2014 (1)
- July 2014 (3)
- March 2014 (6)
- February 2014 (4)
- November 2013 (3)
- October 2013 (4)
- September 2013 (2)
- July 2013 (2)
- June 2013 (2)
- May 2013 (1)
- April 2013 (6)
- March 2013 (3)
- February 2013 (8)
- January 2013 (5)
- December 2012 (1)
- November 2012 (6)
- October 2012 (7)
- August 2012 (1)
- July 2012 (9)
- June 2012 (1)
- April 2012 (1)
- December 2011 (2)
- November 2011 (2)