List and String concatenation speed.

Kostirya <[email protected]> Mon, 29 Aug 2016 13:31:01 +0300
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAG+QnTcffmvXcpFv9rivvwdcsRXvUCorYVdEPmhjCE8EHEpOig@mail.gmail.com>
Hello.
I I found that List.@ is very slow. It is slower than:

    fun x @ nil = x
      | x @ y = let
        fun app nil = y
         | app (a :: b) = a :: app b
        in app x end


Here are the benchmark results:

> cat l.sml
val N = 100000
val n = 10000

val l = List.tabulate(n, fn(i) => i)

fun loop 0 f = ()
  | loop i f = (f (); loop (i - 1) f)


open Time

val t0 = now ()
val _ = loop N (fn () => (l @ [0]; ()) )
val t1 = now ()
val _ = print (Real.toString(toReal(Time.-(t1, t0))) ^ "\n")

> mlton l.sml && ./l
12.066907


> cat l.sml
    fun x @ nil = x
      | x @ y = let
        fun app nil = y
         | app (a :: b) = a :: app b
        in app x end

val N = 100000
val n = 10000

val l = List.tabulate(n, fn(i) => i)

fun loop 0 f = ()
  | loop i f = (f (); loop (i - 1) f)


open Time

val t0 = now ()
val _ = loop N (fn () => (l @ [0]; ()) )
val t1 = now ()
val _ = print (Real.toString(toReal(Time.-(t1, t0))) ^ "\n")
> mlton l.sml && ./l
1.934971



String concatenation is slow due to similar reasons.


> cat string_concat.sml
val N = 100000
val n = 10000

val a = "a"
val b = Byte.bytesToString(Word8Vector.tabulate(n, fn(i) =>
Byte.charToByte(#"a")));

fun loop 0 f = ()
  | loop i f = (f (); loop (i - 1) f)


open Time

val t0 = now ()
val _ = loop N (fn () => (a ^ b; ()) )
val t1 = now ()
val _ = print (Real.toString(toReal(Time.-(t1, t0))) ^ "\n")


> poly --script string_concat.sml
0.462269

> mlton string_concat.sml && ./string_concat
2.713323

------------------------------------------------------------------------------

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user