MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Split in Mathematica 2.2

  • To: mathgroup at
  • Subject: [mg12777] Split in Mathematica 2.2
  • From: Andrzej Kozlowski <andrzej at>
  • Date: Wed, 10 Jun 1998 03:04:37 -0400
  • Sender: owner-wri-mathgroup at

Although I have a personal copy of Mathematica 3.0, the university where
I teach still has not upgraded from version 2.2. The result is that I
often find in my classes that certain useful functions are not
available. One of the most useful of them is Split. I therefore decided
to program it myself and the first thing that came to my mind was to
use Fold. I created the following code.

presplit[{x___, l_List}, a_] :=
  If[MemberQ[l, a], {x, Append[l, a]}, {x, l, {a}}] presplit[{}, d_] :=
split[l_List] := Fold[presplit, {}, l]

This works fine:

split[{1, 1, 1, 5, 7, 11, 11, 12, 14, 14}] Out[103]=

Unfortunatelly it is very slow (all tests in Mathematica 3.0 on PowerMac
G3 266):

In[104]:= w = Table[Random[Integer], {10000}];

{76.05 Second,Null}

By comparison the built in function Split gives

{0.483333 Second,Null}

Having read David Wagner's "Power Programming with Mathematica" I
thought I knew the culprit. I decided to get rid of Append and try
using linked lists instead. So I wrote:

presplit1[{x___, l_List}, a_] :=
  If[Not[FreeQ[Last[l], a]], {x, {l, {a}}}, {x, l, {a}}]
   presplit1[{}, d_] := {{d}}
split1[l_List] :=  Map[#/.List -> Sequence&,Fold[presplit1, {}, l], {2}]

I tried my new split1:

{68.9167 Second,Null}

An improvement, but very slight. What is making this so slow? Can anyone
suggest a really fast implementation of Split that works in version

Andrzej Kozlowski

  • Prev by Date: Exporting lists from Mathematica 3.0 to Excel
  • Next by Date: Easy way to autoload packages.
  • Previous by thread: Re: Exporting lists from Mathematica 3.0 to Excel
  • Next by thread: Re: Split in Mathematica 2.2