首页 \ 问答 \ 为什么从Data.Text构建一个Haskell字符串非常缓慢(Why is building a Haskell String from Data.Text so slow)

为什么从Data.Text构建一个Haskell字符串非常缓慢(Why is building a Haskell String from Data.Text so slow)

所以我有一个地点类

data Location = Location {
    title :: String
  , description :: String
  }

instance Show Location where
  show l = title l ++ "\n"
         ++ replicate (length $ title l) '-' ++ "\n"
         ++ description l

然后我将其更改为使用Data.Text

data Location = Location {
    title :: Text
  , description :: Text
  }

instance Show Location where
  show l = T.unpack $
    title l <> "\n"
    <> T.replicate (T.length $ title l) "-" <> "\n"
    <> description l

使用标准,我对showData.Text实现上show的时间进行了基准测试:

benchmarks = [ bench "show" (whnf show l) ]
  where l = Location {
                title="My Title"
              , description = "This is the description."
              }

String实现花费了34ns, Data.Text实现速度几乎了六倍,达到了170ns

如何使Data.Text工作速度与String类似?

编辑:愚蠢的错误

我不确定这是怎么发生的,但我无法复制原来的速度差异:现在对于字符串和文本我分别获得了28ns和24ns

对于更具侵略性的bench "length.show" (whnf (length . show) l)基准,对于String和Text,我分别得到467ns和3954ns。

如果我使用非常基本的懒惰生成器,没有复制的破折号

import qualified Data.Text.Lazy.Builder as Bldr

instance Show Location where
  show l = show $
    Bldr.fromText (title l) <> Bldr.singleton '\n'
  --  <> Bldr.fromText (T.replicate (T.length $ title l) "-") <> Bldr.singleton '\n'
    <> Bldr.fromText (description l)

并尝试原版,普通show基准,我获得19ns。 现在,这是错误的,因为使用show将构建器转换为String将会转义换行符。 如果我用LT.unpack $ Bldr.toLazyText替换它,其中LTData.Text.Lazy的限定导入,那么我得到192ns。

我正在Mac笔记本电脑上测试这个,我怀疑我的计时受到机器噪音的严重损坏。 感谢您的指导。


So I had a location class

data Location = Location {
    title :: String
  , description :: String
  }

instance Show Location where
  show l = title l ++ "\n"
         ++ replicate (length $ title l) '-' ++ "\n"
         ++ description l

Then I changed it to use Data.Text

data Location = Location {
    title :: Text
  , description :: Text
  }

instance Show Location where
  show l = T.unpack $
    title l <> "\n"
    <> T.replicate (T.length $ title l) "-" <> "\n"
    <> description l

Using criterion, I benchmarked the time taken by show on both the String and Data.Text implementations:

benchmarks = [ bench "show" (whnf show l) ]
  where l = Location {
                title="My Title"
              , description = "This is the description."
              }

The String implementation took 34ns, the Data.Text implementation was almost six times slower, at 170ns

How do I get Data.Text working as fast as String?

Edit: Silly mistakes

I'm not sure how this happened, but I cannot replicate the original speed difference: now for String and Text I get 28ns and 24ns respectively

For the more aggressive bench "length.show" (whnf (length . show) l) benchmark, for String and Text, I get 467ns and 3954ns respectively.

If I use a very basic lazy builder, without the replicated dashes

import qualified Data.Text.Lazy.Builder as Bldr

instance Show Location where
  show l = show $
    Bldr.fromText (title l) <> Bldr.singleton '\n'
  --  <> Bldr.fromText (T.replicate (T.length $ title l) "-") <> Bldr.singleton '\n'
    <> Bldr.fromText (description l)

and try the original, ordinary show benchmark, I get 19ns. Now this is buggy, as using show to convert a builder to a String will escape newlines. If I replace it with LT.unpack $ Bldr.toLazyText, where LT is a qualified import of Data.Text.Lazy, then I get 192ns.

I'm testing this on a Mac laptop, and I suspect my timings are getting horribly corrupted by machine noise. Thanks for the guidance.


原文:https://stackoverflow.com/questions/33710015
更新时间:2021-12-04 14:12

最满意答案

是! 使用overflow: auto;word-break: break-all;

div {
    ...
    overflow-y: auto;
    overflow-x: hidden;
    word-break: break-all;
}

工作小提琴: http : //jsfiddle.net/631msL3L/2/

注意:Opera不支持分word-break


Yes! Use overflow: auto; and word-break: break-all;.

div {
    ...
    overflow-y: auto;
    overflow-x: hidden;
    word-break: break-all;
}

Working fiddle: http://jsfiddle.net/631msL3L/2/

Note: word-break is not supported by Opera.

相关问答

更多
  • 问题解决了! 这是我的代码:我已将此代码添加到包含文本的div中 CSS: -moz-column-count:1; -webkit-column-count:1; column-count:1; -moz-column-gap:30px; -webkit-column-gap:30px; column-gap:30px; Problem Solved! Here is my code: I have added this code to the div containing the text CSS: ...
  • 你必须添加white-space: nowrap; 容器的样式以及text-overflow:ellipsis; 。 看到这里 编辑 这里有更多信息。 You have to add the white-space: nowrap; style to the container along with the text-overflow:ellipsis;. See here Edit Here's some more info on it.
  • 删除top: -61px; 来自div.ad_banner 。 此外,如果你有一个加载屏幕我会建议有一些简单的动画,因为它看起来不像页面已经死亡。 SOLUTION: ok this issue was bugging me for awhile, and i don't understand why this is. i noticed some extra 1 or 2 pixels of whitespace between the sales_ph_num div and the nav_bar d ...
  • 你只需要使用box-sizing:border-box html, body { height: 100%; margin: 0 } .blah { display: flex; flex-direction: column; justify-content: center; text-align: center; background: #ffb3b3; min-height: 100%; padding: 10% 0; box-sizin ...
  • 为什么不只是在img上指定高度? http://jsfiddle.net/HMswX/2/ 否则,请不要在标题上放置高度.. http://jsfiddle.net/HMswX/3/ 根据你的更新.. 保证金不工作,因为div崩溃了..看看这个 ...
  • QueryPerformanceCounter因其不可靠性而臭名昭着。 如果您准备处理异常结果,可以使用个别短时间间隔计时很好。 这不是确切的 - 它通常基于PCI总线频率,并且负载很重的总线可能导致丢失滴答声。 GetTickCount实际上更稳定,如果你叫做timeBeginPeriod ,可以给你1ms的分辨率。 它最终将包装,所以你需要处理。 不应使用__rdtsc ,除非您正在分析并控制您正在运行哪个核心并准备好处理可变CPU频率。 GetSystemTime适合较长时间的测量,但会在系统时间调整 ...
  • 是! 使用overflow: auto; 和word-break: break-all; 。 div { ... overflow-y: auto; overflow-x: hidden; word-break: break-all; } 工作小提琴: http : //jsfiddle.net/631msL3L/2/ 注意:Opera不支持分word-break 。 Yes! Use overflow: auto; and word-break: break-all;. ...
  • 在css元素中使用相对单位(em,rem)可以帮助防止这种情况发生。 还要避免在元素上使用设置宽度,因为溢出的文本可能会被截断。 WCAG AA标准规定用户必须能够在不丢失内容或功能的情况下将文本大小调整至200%,因此确保任何按钮随着文本的增加而增长非常重要。 您可以消除按钮上可能存在的任何设置宽度,否则它们应相对于其中的文本进行扩展。 wcag标准的来源: https ://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-scale.ht ...
  • 我想我终于明白了你想要实现的目标。 您希望图像大小适应,因此它适合蓝色粘性块,而此块保持100px高。 是对的吗 ? 如果是这样,您可以使用flexbox执行此操作。 但要小心:所有浏览器都不支持它。 但也不是position: sticky 。 .sticky { /* Everything you already have here... */ display: flex; flex-direction: column; } https://codepen.io/anon/pe ...
  • 我做了一个jsFiddle解决方案 当一个元素溢出他的父元素时,正常的行为是它只溢出到右边。 例如,当您的网站比视口宽时,您不必向左滚动,而只向右滚动。 该解决方案基于绝对居中的div,具有负的左边距(该值是他自己宽度的一半)。 所以如果你知道这个元素的宽度,这个解决方案应该没问题。 在FF 3.6,IE7和IE8中测试过 I've made a jsFiddle solution When an element overflows his parent, it is normal behaviour th ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)