Extending Jeff's answer
Different overheads hurt for small sizes and help for large sizes.
dt = New Table( "Untitled 3",
Add Rows( 0 ),
New Column( "size", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "for", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "foreach", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "custom", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) )
);
NumListToChar = Function( {myList},
{defaultlocal},
dtx = New Table( "x", New Column( "y", values( myList ) ), private );
dtx:y << data type( character );
myList = dtx:y << get values;
Close( dtx, nosave );
Return( myList );
);
For( listsize = 1, listsize < 1e7, listsize *= 2,
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
For Each( {value, index}, theList, theList[index] = Char( value ) );
foreachloop = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
theList = NumListToChar( theList );
customfunction = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
For( i = 1, i <= N Items( theList ), i++,
theList[i] = Char( theList[i] )
);
forloop = HP Time() - start;
Show( foreachloop, customfunction, forloop );
dt << addrows( 1 );
dt:size[N Rows( dt )] = listsize;
dt:for[N Rows( dt )] = forloop / 1e6;
dt:foreach[N Rows( dt )] = foreachloop / 1e6;
dt:custom[N Rows( dt )] = customfunction / 1e6;
Wait( 0.1 ); // so it shows on display
);
Craige