我目前正在使用这个:
$('#login2').addClass('inactive');
$('#login2>input').attr("disabled", 'true');
任何让它更快的方法?
还是以其他方式更快地生成字符串?
答案 0 :(得分:0)
通过更有效地使用数据(和控制)结构可以获得很小的性能提升。
(defconst our-charset "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
(defconst our-charset-length (length our-charset))
(defun my-generate-string (&optional max-length min-length)
"Generate a random string."
(let (string)
(dotimes (_i (+ (random (- (or max-length 10) (or min-length 5) -1)) (or min-length 5)))
(push (aref our-charset (random our-charset-length)) string))
(concat string)))
(defun our-generate-string (&optional max-length min-length)
(let* ((max-length (or max-length 10))
(min-length (or min-length 5))
(length (+ min-length (random (- max-length min-length -1))))
(string (make-string length ?0)))
(dotimes (i length string)
(aset string i
(aref our-charset (random our-charset-length))))))
(defmacro measure-time (&rest body)
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(/
(apply #'+
(mapcar
(lambda (ignore) (measure-time (my-generate-string 1000 999)))
(number-sequence 0 999)))
1000)
;; 0.0018966329990000002
(/
(apply #'+
(mapcar
(lambda (ignore) (measure-time (our-generate-string 1000 999)))
(number-sequence 0 999)))
1000)
;; 0.0009833975549999997
您的搜索结果可能因硬件和随机性而异,但our-generate-string
应该更快。
您可能还想稍微调整一下您的调用约定。
(defun our-randomized-string (charset length)
(let ((charset-length (length charset))
(string (make-string length ?0)))
(dotimes (i length string)
(aset string i
(aref charset (random charset-length))))))
(/
(apply #'+
(mapcar
(lambda (ignore) (measure-time (our-randomized-string our-charset 1000)))
(number-sequence 0 999)))
1000)
;; 0.0009300809320000015
请注意,our-randomized-string
不一定比our-generate-string
更快,但能够修复函数外的参数而不是确定它们可能最终会带来好处。在这个版本中,字符集也可以比在另一个版本中更容易交换。