-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve lazy performance of
Data.Text.Lazy.inits
The previous implementation, itself based on an earlier version of `Data.List.inits`, inherited the flaw that accessing the i-th element took quadratic time O(i²). This now takes linear time O(i) as expected. The current version of `Data.List.inits` uses a banker's queue to obtain good performance when generating very long lists. For lazy text, consisting of a few big chunks, that benefit seems negligible. So I chose a simpler implementation.
- Loading branch information
Showing
4 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- | Benchmarks on artificial data. | ||
|
||
module Benchmarks.Micro (benchmark) where | ||
|
||
import qualified Data.Text.Lazy as TL | ||
import qualified Data.Text as T | ||
import Test.Tasty.Bench (Benchmark, bgroup, bench, nf) | ||
|
||
benchmark :: Benchmark | ||
benchmark = bgroup "Micro" | ||
[ -- Accessing i-th element should take O(i) time. | ||
-- The 2k case should run in 2x the time of the 1k case. | ||
bgroup "Lazy.inits" | ||
[ bench "last 1k" $ nf (last . TL.inits) (chunks 1000) | ||
, bench "last 2k" $ nf (last . TL.inits) (chunks 2000) | ||
, bench "map-take1 1k" $ nf (map (TL.take 1) . TL.inits) (chunks 1000) | ||
, bench "map-take1 2k" $ nf (map (TL.take 1) . TL.inits) (chunks 2000) | ||
] | ||
] | ||
|
||
chunks :: Int -> TL.Text | ||
chunks n = TL.fromChunks (replicate n (T.pack "a")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters