Repeat a string: Difference between revisions

(→‎{{header|OCaml}}: Bytes instead of String)
Line 1,487:
// Slower version
func repeatString(n: Int) -> String {
return "".join(Array(count: n, repeatedValue: self).joinWithSeparator("")
}
 
// Faster version
// benchmarked with a 1000 characters and 100 repeats the fast version is approx 500 000 times faster :-)
func repeatrepeatString2(n:Int) -> String {
var result = self
for _ in 1 ..< n {
result.extendappendContentsOf(self) // Note that String.extend is up to 10 times faster than "result += self"
}
return result
Line 1,501:
}
 
printlnprint( "ha".repeatString(5) )
printlnprint( "he".repeatrepeatString2(5) )</lang>
{{out}}
<pre>
hahahahaha
Anonymous user