首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的换能器功能比使用->>操作符慢?

为什么我的换能器功能比使用->>操作符慢?
EN

Stack Overflow用户
提问于 2018-02-09 13:28:55
回答 1查看 493关注 0票数 5

在解决Hackkerank (https://www.hackerrank.com/challenges/string-compression/problem)的问题时,我编写了两个带有和不带转换器的实现。

我原以为换能器的实现比函数链式操作符->>更快。不幸的是,根据我的迷你基准,链式操作人员的性能比传感器高2.5倍.

我在想,只要有可能,我就应该使用传感器。还是我没有正确理解换能器的概念?

时间:

经过的时间: 0.844459毫秒 经过的时间: 2.697836毫秒

代码:

代码语言:javascript
复制
(defn string-compression-2
  [s]
  (->> s
       (partition-by identity)
       (mapcat #(if (> (count %) 1)
               (list (first %) (count %))
               (list (first %))))
       (apply str)))

(def xform-str-compr
  (comp (partition-by identity)
        (mapcat #(if (> (count %) 1)
                (list (first %) (count %))
                (list (first %))))))

(defn string-compression-3
  [s]
  (transduce xform-str-compr str s))

(time (string-compression-2 "aaabccdddd"))
(time (string-compression-3 "aaabccdddd"))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-09 13:46:10

根据黄曲霉的说法,换能器的版本确实更快。

代码语言:javascript
复制
(crit/quick-bench (string-compression-2 "aaabccdddd"))
             Execution time mean : 6.150477 µs
    Execution time std-deviation : 246.740784 ns
   Execution time lower quantile : 5.769961 µs ( 2.5%)
   Execution time upper quantile : 6.398563 µs (97.5%)
                   Overhead used : 1.620718 ns

(crit/quick-bench (string-compression-3 "aaabccdddd"))
             Execution time mean : 2.533919 µs
    Execution time std-deviation : 157.594154 ns
   Execution time lower quantile : 2.341610 µs ( 2.5%)
   Execution time upper quantile : 2.704182 µs (97.5%)
               Overhead used : 1.620718 ns

正如coredump评论的那样,一个样本的大小并不足以说明一种方法是否比另一种方法更快。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48706991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档