at first, i would like to thank all the people here. i'm working with mathematica for half a year now and always found great advice and tricks.
now, i came up with a problem i couldn't find a solution for. from a file, i import a string with around 23 mio. characters which is reasonably fast (it acutually is a dna sequence). i then want to split this string into its characters and convert each of them to an integer number.
my first, naive approach was the following:
MaxMemoryUsed[]/1024^2//N
AbsoluteTiming[list=Characters[a]/.{"A"->1,"T"->2,"G"->3,"C"->4};]
ByteCount[list]/1024^2//N
MaxMemoryUsed[]/1024^2//N
which creates the following output:
134.286
{13.301206,Null}
702.261
1976.42
as you can see, the timing is quite good but the memory usage during the process is horrible. my next approach was reading from a stream and convert each character to an integer instantly:
MaxMemoryUsed[]/1024^2//N
string=StringReplace[string,{"A"->"1","T"->"2","G"->"3","C"->"4"}];
str=StringToStream[string];
AbsoluteTiming[list=Table[ToExpression[Read[str,Character]],{i,1,StringLength[string]}];]
Close[str];
ByteCount[list]/1024^2//N
MaxMemoryUsed[]/1024^2//N
which results in
134.281
{176.502736,Null}
702.252
813.208
as yo can see, this approach needs way less memory but takes considerably longer. my question now is, if there is another memory efficient way to do this which is faster than my second try?
thanks a lot!
ToExpression@StringCases[string, DigitCharacter]
, orToCharacterCode@string - 48
$\endgroup$