Created
January 23, 2015 19:04
-
-
Save sam/99cbde217e27262f9b08 to your computer and use it in GitHub Desktop.
Benchmarking various means of collapsing a readable triple-quoted string into a single line appropriate as a portion of a URI.
This file contains hidden or 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
object Main extends testing.Benchmark { | |
val r = """[\r\n\s]""".r | |
val s = """ | |
| Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim | |
| veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea | |
| commodo consequat. Duis aute irure dolor in reprehenderit in voluptate | |
| velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
| cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
""" | |
def collapseMargin(input: String): String = { | |
input.lines.foldLeft(new StringBuilder(input.size)) { (buffer, line) => | |
val len = line.length | |
var startIndex = 0 | |
var endIndex = len | |
while (startIndex < len && line.charAt(startIndex) <= ' ') startIndex += 1 | |
while (endIndex > len && line.charAt(endIndex - 1) <= ' ') endIndex -= 1 | |
buffer append ' ' append line.substring(startIndex, endIndex) | |
}.deleteCharAt(0).mkString | |
} | |
def run = { | |
// r replaceAllIn(s.stripMargin, " ") | |
s.stripMargin.lines.map(_.trim()).mkString(" ") | |
// collapseMargin(s) | |
} | |
} |
This file contains hidden or 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
CACHED REGEX: | |
val r = """[\r\n\s]""".r | |
r replaceAllIn(s.stripMargin, " ") | |
> run 100 10 | |
[info] Running Foo 100 10 | |
Foo$ 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 1 0 0 0 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:53:58 PM | |
> run 100 100 | |
[info] Running Foo 100 100 | |
Foo$ 1 1 1 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:54:05 PM | |
> run 100 1000 | |
[info] Running Foo 100 1000 | |
Foo$ 11 11 11 10 11 10 10 11 11 10 11 11 11 11 11 11 11 11 11 11 10 11 11 11 11 11 11 11 13 10 11 11 10 11 10 11 11 11 11 11 11 10 11 10 11 11 10 11 17 10 11 10 10 10 11 11 11 11 11 11 11 11 11 10 10 11 11 11 10 11 11 11 10 11 11 11 11 11 11 10 11 10 11 11 11 10 11 11 11 11 11 11 11 10 10 11 11 11 11 11 | |
[success] Total time: 6 s, completed Jan 23, 2015 12:54:13 PM | |
> run 100 10000 | |
[info] Running Foo 100 10000 | |
Foo$ 105 109 107 108 107 109 108 109 107 110 107 108 108 11 108 107 107 108 107 107 108 107 107 107 117 108 107 107 12 108 108 107 115 108 108 108 116 107 109 107 118 107 108 10 117 108 107 108 118 108 108 107 120 108 107 107 116 108 10 107 118 107 107 108 117 107 107 107 118 108 107 107 117 10 108 109 121 108 108 107 108 107 108 108 108 107 108 107 10 107 107 107 108 107 107 108 108 107 108 113 | |
[success] Total time: 16 s, completed Jan 23, 2015 12:54:32 PM | |
> run 100 100000 | |
[info] Running Foo 100 100000 | |
Foo$ 1073 1098 1106 1106 1106 1106 1105 1134 1104 1115 1107 1195 1189 1176 1114 1117 1099 1106 1115 1104 1108 1120 1105 1101 1107 1104 1106 1136 1117 1097 1103 1118 1115 1107 1108 1111 1096 1110 1104 1113 1107 1108 1097 1098 1107 1106 1106 1107 1103 1106 1110 1117 1109 1106 1112 1106 1097 1114 1109 1113 1108 1114 1101 1098 1115 1118 1108 1109 1108 1102 1099 1114 1121 1108 1107 1108 1098 1098 1107 1114 1112 1109 1103 1098 1103 1106 1104 1112 1113 1104 1104 1123 1113 1112 1117 1111 1107 1100 1116 1117 | |
[success] Total time: 116 s, completed Jan 23, 2015 12:56:52 PM | |
ITERATOR MAP: s.stripMargin.lines.map(_.trim()).mkString(" ") | |
> run 100 10 | |
[info] Running Foo 100 10 | |
Foo$ 1 1 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:59:47 PM | |
> run 100 100 | |
[info] Running Foo 100 100 | |
Foo$ 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 | |
[success] Total time: 6 s, completed Jan 23, 2015 1:00:01 PM | |
> run 100 1000 | |
[info] Running Foo 100 1000 | |
Foo$ 11 3 2 3 2 2 2 3 2 2 3 2 3 2 2 3 2 2 2 2 2 3 2 2 2 3 3 2 2 3 2 2 2 2 4 3 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 3 2 3 2 3 2 2 2 3 2 3 2 2 2 2 2 2 3 2 3 2 3 2 2 2 3 2 2 2 2 2 2 2 2 | |
[success] Total time: 6 s, completed Jan 23, 2015 1:00:09 PM | |
> run 100 10000 | |
[info] Running Foo 100 10000 | |
Foo$ 27 23 23 23 23 23 24 23 24 23 24 23 23 23 23 23 24 24 34 24 24 23 23 23 23 23 29 23 23 23 23 23 23 23 23 24 23 23 23 23 23 23 23 23 23 24 23 23 23 23 23 25 23 23 23 23 23 23 23 30 23 23 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 28 23 23 23 23 23 23 23 28 24 23 23 23 23 23 23 | |
[success] Total time: 8 s, completed Jan 23, 2015 1:00:18 PM | |
> run 100 100000 | |
[info] Running Foo 100 100000 | |
Foo$ 244 247 233 247 232 232 238 233 242 232 244 233 248 23 245 232 241 232 233 234 232 242 232 243 232 242 233 241 23 237 233 232 255 233 240 232 243 232 242 233 242 232 233 23 233 243 233 243 232 242 232 241 232 233 234 232 242 237 24 232 242 234 241 233 243 231 232 235 236 244 232 245 231 24 232 241 232 234 235 232 242 232 242 231 243 233 241 232 24 233 232 234 232 240 232 242 235 244 232 242 | |
[success] Total time: 29 s, completed Jan 23, 2015 1:00:49 PM | |
SINGLE ITERATION WITH SUBSTRING TRIM: | |
def collapseMargin(input: String): String = { | |
input.lines.foldLeft(new StringBuilder(input.size)) { (buffer, line) => | |
val len = line.length | |
var startIndex = 0 | |
var endIndex = len | |
while (startIndex < len && line.charAt(startIndex) <= ' ') startIndex += 1 | |
while (endIndex > len && line.charAt(endIndex - 1) <= ' ') endIndex -= 1 | |
buffer append ' ' append line.substring(startIndex, endIndex) | |
}.deleteCharAt(0).mkString | |
} | |
> run 100 10 | |
[info] Running Foo 100 10 | |
Foo$ 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:50:01 PM | |
> run 100 100 | |
[info] Running Foo 100 100 | |
Foo$ 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:50:08 PM | |
> run 100 1000 | |
[info] Running Foo 100 1000 | |
Foo$ 3 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 1 2 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 | |
[success] Total time: 5 s, completed Jan 23, 2015 12:50:15 PM | |
> run 100 10000 | |
[info] Running Foo 100 10000 | |
Foo$ 21 13 12 12 11 11 12 12 11 11 12 12 11 11 12 11 12 12 11 12 11 11 11 11 15 14 11 12 12 11 11 11 11 12 11 20 11 12 12 11 11 11 11 11 11 11 17 12 11 11 11 11 11 11 11 11 11 18 12 12 11 12 12 12 11 12 12 11 14 11 12 11 11 12 11 11 11 11 12 11 12 11 12 11 11 11 11 12 11 11 20 11 12 12 12 11 11 12 12 11 | |
[success] Total time: 6 s, completed Jan 23, 2015 12:50:24 PM | |
> run 100 100000 | |
[info] Running Foo 100 100000 | |
Foo$ 155 157 118 115 125 116 115 116 125 117 115 116 126 11 116 115 126 115 115 115 130 116 115 115 117 115 116 115 11 115 115 124 116 115 116 126 116 115 115 126 115 116 115 12 116 115 115 127 115 116 115 118 116 115 115 117 115 115 11 116 115 115 125 115 115 115 124 115 115 115 124 115 115 11 124 115 115 115 124 115 115 115 125 115 115 115 117 115 11 115 116 116 115 128 115 115 115 128 115 115 | |
[success] Total time: 16 s, completed Jan 23, 2015 12:50:42 PM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment