lisp线型center未定义_lisp-flag(bandera)不起作用

实际上,不需要外部定义的循环来查明字符串是否是回文的。[备注:嗯,我一开始就这么认为。但正如@coredump和@jkiiski指出的那样,

reverse

函数使过程变慢,因为它只复制一次整个字符串。]

用途:

(defun palindromep (s)

(string= s (reverse s)))

它又回来了

T

如果S是回文,则为

NIL

(不是这样,它只会节省你的写作努力,但比使用程序效率低

loop

)

详细版本为:

(defun palindromep (s)

(let ((result (string= s (reverse s))))

(write (if result

"Is a palindrome"

"Is not a palindrome"))

result))

写下你想要的答案,但返回

T

返回的测试函数的命名约定

T

名字的结尾是

p

对于“谓词”。

与@coredump建议的while循环相比,reverse函数的性能更低。

这是我的初学者尝试测试速度[不推荐]:

;; Improved loop version by @coredump:

(defun palindromep-loop (string)

(loop with max = (1- (length string))

for i from 0

for j downfrom max

while (< i j)

always (char= (char string i)

(char string j))))

;; the solution with reverse

(defun palindromep (s)

(string= s (reverse s)))

;; the test functions test over and over the same string "abcdefggfedcba"

;; 10000 times over and over again

;; I did the repeats so that the measuring comes at least to the milliseconds

;; range ... (but it was too few repeats still. See below.)

(defun test-palindrome-loop ()

(loop repeat 10000

do (palindromep-loop "abcdefggfedcba")))

(time (test-palindrome-loop))

(defun test-palindrome-p ()

(loop repeat 10000

do (palindromep "abcdefggfedcba")))

(time (test-palindrome-p))

;; run on SBCL

[55]> (time (test-palindrome-loop))

Real time: 0.152438 sec.

Run time: 0.152 sec.

Space: 0 Bytes

NIL

[56]> (time (test-palindrome-p))

Real time: 0.019284 sec.

Run time: 0.02 sec.

Space: 240000 Bytes

NIL

;; note: this is the worst case that the string is a palindrome

;; for `palindrome-p` it would break much earlier when a string is

;; not a palindrome!

这是@coredump试图测试函数的速度:

(lisp-implementation-type)

"SBCL"

(lisp-implementation-version)

"1.4.0.75.release.1710-6a36da1"

(machine-type)

"X86-64"

(defun palindromep-loop (string)

(loop with max = (1- (length string))

for i from 0

for j downfrom max

while (< i j)

always (char= (char string i)

(char string j))))

(defun palindromep (s)

(string= s (reverse s)))

(defun test-palindrome-loop (s)

(sb-ext:gc :full t)

(time

(loop repeat 10000000

do (palindromep-loop s))))

(defun test-palindrome-p (s)

(sb-ext:gc :full t)

(time

(loop repeat 10000000

do (palindromep s))))

(defun rand-char ()

(code-char

(+ #.(char-code #\a)

(random #.(- (char-code #\z) (char-code #\a))))))

(defun generate-palindrome (n &optional oddp)

(let ((left (coerce (loop repeat n collect (rand-char)) 'string)))

(concatenate 'string

left

(and oddp (list (rand-char)))

(reverse left))))

(let ((s (generate-palindrome 20)))

(test-palindrome-p s)

(test-palindrome-loop s))

Evaluation took:

4.093 seconds of real time

4.100000 seconds of total run time (4.068000 user, 0.032000 system)

[ Run times consist of 0.124 seconds GC time, and 3.976 seconds non-GC time. ]

100.17% CPU

9,800,692,770 processor cycles

1,919,983,328 bytes consed

Evaluation took:

2.353 seconds of real time

2.352000 seconds of total run time (2.352000 user, 0.000000 system)

99.96% CPU

5,633,385,408 processor cycles

0 bytes consed

从中我学到了:

-更严格地测试,必要时重复(秒范围)

-随机生成并并行测试

非常感谢你的好例子@coredump!还有你说的“JKIISKI”!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值