为什么从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
使用标准,我对
show
和Data.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
替换它,其中LT
是Data.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 theString
andData.Text
implementations:benchmarks = [ bench "show" (whnf show l) ] where l = Location { title="My Title" , description = "This is the description." }
The
String
implementation took 34ns, theData.Text
implementation was almost six times slower, at 170nsHow do I get
Data.Text
working as fast asString
?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 usingshow
to convert a builder to a String will escape newlines. If I replace it withLT.unpack $ Bldr.toLazyText
, whereLT
is a qualified import ofData.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
最满意答案
是! 使用
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;
andword-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.
-
内容溢出div(content overflows div)[2022-11-02]
删除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 ...
-
div中的图像标记溢出(Image tag in the div overflows)[2021-08-14]
为什么不只是在img上指定高度? http://jsfiddle.net/HMswX/2/ 否则,请不要在标题上放置高度.. http://jsfiddle.net/HMswX/3/ 根据你的更新.. 保证金不工作,因为div崩溃了..看看这个 ... -
QueryPerformanceCounter因其不可靠性而臭名昭着。 如果您准备处理异常结果,可以使用个别短时间间隔计时很好。 这不是确切的 - 它通常基于PCI总线频率,并且负载很重的总线可能导致丢失滴答声。 GetTickCount实际上更稳定,如果你叫做timeBeginPeriod ,可以给你1ms的分辨率。 它最终将包装,所以你需要处理。 不应使用__rdtsc ,除非您正在分析并控制您正在运行哪个核心并准备好处理可变CPU频率。 GetSystemTime适合较长时间的测量,但会在系统时间调整 ...
-
文本在div上溢出(Text overflows on a div)[2023-11-21]
是! 使用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 ...
-
居中在另一个居中的div内溢出它的父母的div(Centering an div which overflows it's parent within another centered div)[2021-09-06]
我做了一个jsFiddle解决方案 当一个元素溢出他的父元素时,正常的行为是它只溢出到右边。 例如,当您的网站比视口宽时,您不必向左滚动,而只向右滚动。 该解决方案基于绝对居中的div,具有负的左边距(该值是他自己宽度的一半)。 所以如果你知道这个元素的宽度,这个解决方案应该没问题。 在FF 3.6,IE7和IE8中测试过 I've made a jsFiddle solution When an element overflows his parent, it is normal behaviour th ...