File size distribution: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: use Data.Map. Some other optimizations.)
m (→‎{{header|Haskell}}: increase the precision when displaying file sizes)
Line 504: Line 504:
displaySize :: Integer -> String
displaySize :: Integer -> String
displaySize n
displaySize n
| n <= 2^10 = show n <> "B"
| n <= 2^10 = printf "%8dB " n
| n >= 2^10 && n <= 2^20 = display "KB" $ 2^10
| n >= 2^10 && n <= 2^20 = display (2^10) "KB"
| n >= 2^20 && n <= 2^30 = display "MB" $ 2^20
| n >= 2^20 && n <= 2^30 = display (2^20) "MB"
| n >= 2^30 && n <= 2^40 = display "GB" $ 2^30
| n >= 2^30 && n <= 2^40 = display (2^30) "GB"
| n >= 2^40 && n <= 2^50 = display "TB" $ 2^40
| n >= 2^40 && n <= 2^50 = display (2^40) "TB"
| otherwise = "Too large!"
| otherwise = printf "%8dB " n
where
where
display suffix = (<> suffix) . show . round . (realToFrac n /)
-- display suffix = (<> suffix) . show . (realToFrac n /)
display :: Double -> String -> String
display b = printf "%7.2f%s " (realToFrac n / b)


folderWorker :: Chan FilePath -> Chan [Item] -> IO ()
folderWorker :: Chan FilePath -> Chan [Item] -> IO ()
Line 531: Line 533:


displayFrequency :: Integer -> FrequencyGroup -> IO ()
displayFrequency :: Integer -> FrequencyGroup -> IO ()
displayFrequency filesCount ((min, max), count) =
displayFrequency filesCount ((min, max), count) = do
printf "%5s <-> %5s = %-10d %6.3f%%: %-5s\n" (displaySize min)
printf "%s <-> %s" (displaySize min) (displaySize max)
(displaySize max) count percentage bars
printf "= %-10d %6.3f%%: %-5s\n" count percentage bars
where
where
percentage :: Double
percentage :: Double
Line 579: Line 581:
printf "Total size: %s\n" $ displaySize $ totalBytes items
printf "Total size: %s\n" $ displaySize $ totalBytes items
putStrLn "\nDistribution:\n"
putStrLn "\nDistribution:\n"
printf "%5s <-> %4s %8s\n" "From" "To" "Count"
printf "%9s <-> %9s %7s\n" "From" "To" "Count"
putStrLn $ replicate 37 '-'
putStrLn $ replicate 45 '-'
let results = expandedGroups groupSize (sizes items) (groupThreshold fileCount) items
let results = expandedGroups groupSize (sizes items) (groupThreshold fileCount) items
-- let results = initialGroups groupSize items
-- let results = initialGroups groupSize items
Line 591: Line 593:
expandGroups gsize sizes n . initialGroups gsize</lang>
expandGroups gsize sizes n . initialGroups gsize</lang>
{{out}}
{{out}}
<pre style="height: 50rem;">$ filedist ~/Music
<pre style="height: 50rem;">$ filedist ~/Music
Using 4 worker threads
Using 4 worker threads
Total files: 688
Total files: 688
Total folders: 663
Total folders: 663
Total size: 986MB
Total size: 985.85MB


Distribution:
Distribution:


From <-> To Count
From <-> To Count
-------------------------------------
---------------------------------------------
0B <-> 80B = 7 1.017%: █
0B <-> 80B = 7 1.017%: █
81B <-> 161B = 74 10.756%: ███████████
81B <-> 161B = 74 10.756%: ███████████
162B <-> 242B = 112 16.279%: ████████████████
162B <-> 242B = 112 16.279%: ████████████████
243B <-> 323B = 99 14.390%: ██████████████
243B <-> 323B = 99 14.390%: ██████████████
322B <-> 643B = 23 3.343%: ███
323B <-> 645B = 23 3.343%: ███
644B <-> 965B = 2 0.291%:
646B <-> 968B = 2 0.291%:
966B <-> 1KB = 1 0.145%:
969B <-> 1.26KB = 1 0.145%:
3KB <-> 6KB = 12 1.744%: ██
3.19KB <-> 6.38KB = 12 1.744%: ██
6KB <-> 10KB = 22 3.198%: ███
6.38KB <-> 9.58KB = 22 3.198%: ███
10KB <-> 13KB = 12 1.744%: ██
9.58KB <-> 12.77KB = 12 1.744%: ██
14KB <-> 27KB = 15 2.180%: ██
13.52KB <-> 27.04KB = 15 2.180%: ██
27KB <-> 41KB = 6 0.872%: █
27.04KB <-> 40.57KB = 6 0.872%: █
41KB <-> 54KB = 22 3.198%: ███
40.57KB <-> 54.09KB = 22 3.198%: ███
54KB <-> 108KB = 99 14.390%: ██████████████
54.20KB <-> 108.41KB = 99 14.390%: ██████████████
108KB <-> 163KB = 23 3.343%: ███
108.41KB <-> 162.61KB = 23 3.343%: ███
163KB <-> 217KB = 8 1.163%: █
162.61KB <-> 216.81KB = 8 1.163%: █
236KB <-> 473KB = 3 0.436%:
236.46KB <-> 472.93KB = 3 0.436%:
709KB <-> 946KB = 44 6.395%: ██████
709.39KB <-> 945.85KB = 44 6.395%: ██████
3MB <-> 5MB = 4 0.581%: █
3.30MB <-> 4.96MB = 4 0.581%: █
5MB <-> 7MB = 21 3.052%: ███
4.96MB <-> 6.61MB = 21 3.052%: ███
7MB <-> 13MB = 72 10.465%: ██████████
6.67MB <-> 13.33MB = 72 10.465%: ██████████
13MB <-> 20MB = 6 0.872%: █
13.33MB <-> 20.00MB = 6 0.872%: █
20MB <-> 27MB = 1 0.145%:
20.00MB <-> 26.66MB = 1 0.145%:


$ filedist ~/Music 10
$ filedist ~/Music 10
Line 629: Line 631:
Total files: 688
Total files: 688
Total folders: 663
Total folders: 663
Total size: 986MB
Total size: 985.85MB


Distribution:
Distribution:


From <-> To Count
From <-> To Count
-------------------------------------
---------------------------------------------
0B <-> 88B = 7 1.017%: █
0B <-> 88B = 7 1.017%: █
89B <-> 177B = 75 10.901%: ███████████
89B <-> 177B = 75 10.901%: ███████████
178B <-> 266B = 156 22.674%: ███████████████████████
178B <-> 266B = 156 22.674%: ███████████████████████
267B <-> 355B = 57 8.285%: ████████
267B <-> 355B = 57 8.285%: ████████
356B <-> 444B = 20 2.907%: ███
356B <-> 444B = 20 2.907%: ███
801B <-> 889B = 2 0.291%:
801B <-> 889B = 2 0.291%:
959B <-> 2KB = 1 0.145%:
959B <-> 1.87KB = 1 0.145%:
4KB <-> 5KB = 1 0.145%:
3.75KB <-> 4.68KB = 1 0.145%:
5KB <-> 6KB = 1 0.145%:
4.68KB <-> 5.62KB = 1 0.145%:
6KB <-> 7KB = 11 1.599%: ██
5.62KB <-> 6.55KB = 11 1.599%: ██
7KB <-> 7KB = 10 1.453%: █
6.56KB <-> 7.49KB = 10 1.453%: █
7KB <-> 8KB = 4 0.581%: █
7.49KB <-> 8.43KB = 4 0.581%: █
8KB <-> 9KB = 7 1.017%: █
8.43KB <-> 9.36KB = 7 1.017%: █
9KB <-> 19KB = 21 3.052%: ███
9.43KB <-> 18.85KB = 21 3.052%: ███
19KB <-> 28KB = 6 0.872%: █
18.85KB <-> 28.28KB = 6 0.872%: █
28KB <-> 38KB = 4 0.581%: █
28.28KB <-> 37.71KB = 4 0.581%: █
38KB <-> 47KB = 12 1.744%: ██
37.71KB <-> 47.13KB = 12 1.744%: ██
47KB <-> 57KB = 16 2.326%: ██
47.13KB <-> 56.56KB = 16 2.326%: ██
57KB <-> 66KB = 23 3.343%: ███
56.56KB <-> 65.99KB = 23 3.343%: ███
66KB <-> 75KB = 26 3.779%: ████
65.99KB <-> 75.41KB = 26 3.779%: ████
75KB <-> 85KB = 15 2.180%: ██
75.41KB <-> 84.84KB = 15 2.180%: ██
85KB <-> 94KB = 17 2.471%: ██
84.84KB <-> 94.27KB = 17 2.471%: ██
95KB <-> 189KB = 42 6.105%: ██████
94.59KB <-> 189.17KB = 42 6.105%: ██████
189KB <-> 284KB = 4 0.581%: █
189.17KB <-> 283.76KB = 4 0.581%: █
284KB <-> 378KB = 2 0.291%:
283.76KB <-> 378.35KB = 2 0.291%:
851KB <-> 946KB = 44 6.395%: ██████
851.28KB <-> 945.87KB = 44 6.395%: ██████
3MB <-> 5MB = 5 0.727%: █
2.67MB <-> 5.33MB = 5 0.727%: █
5MB <-> 8MB = 41 5.959%: ██████
5.33MB <-> 8.00MB = 41 5.959%: ██████
8MB <-> 11MB = 35 5.087%: █████
8.00MB <-> 10.67MB = 35 5.087%: █████
11MB <-> 13MB = 16 2.326%: ██
10.67MB <-> 13.33MB = 16 2.326%: ██
13MB <-> 16MB = 3 0.436%:
13.33MB <-> 16.00MB = 3 0.436%:
16MB <-> 19MB = 3 0.436%:
16.00MB <-> 18.67MB = 3 0.436%:
24MB <-> 27MB = 1 0.145%:
24.00MB <-> 26.66MB = 1 0.145%:
</pre>
</pre>