FlatString - Faster + Correction
- To: mathgroup <mathgroup at yoda.physics.unc.edu>
- Subject: FlatString - Faster + Correction
- From: HAY at leicester.ac.uk
- Date: Tue, 8 JUN 93 10:26:59 BST
FASTER
Another fast way - using the HoldAll attribute to enable the evaluation
to be tuned to the known sructure gives 5 to 11 times speed up.
Attributes[FlatString] = {HoldAll};
FlatString[s___] :=
Block[{FlatString,temp},
FlatString[
Flatten[
Flatten[ temp, Infinity, FlatString ]/.
FlatString -> List
]/.List -> StringJoin
]/;
Length[(temp={s})] =!=1 || !FreeQ[ temp,FlatString ]
]
Here are some timings, the numbers down the left give the
depth of the FlatString nesting. HS is the RobertoSierra)s
modification of Jason Harris)s contribution, HSH is my further
modification.
HS HSH
1 0.23333 0.05
2 0.48333 0.05
3 0.71667 0.083333
4 0.98333 0.1
5 1.2167 0.13333
6 1.4667 0.15
7 1.7167 0.16667
8 1.9833 0.18333
9 2.25 0.23333
10 2.5 0.26667
CORRECTION
Roberto found that my previous posting gave some very nasty loops.
Changing Head[temp] =!=String to MemberQ[temp, _FlatString]
will, I hope, correct this.
Allan Hayes
hay at leicester.ac.uk