Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following code and it takes like foreeeeever to evaulate:

Table[Table[{If[closeN[[x]] > Max@priceRange[[y]], 1, 
    If[closeN[[x]] < Min@priceRange[[y]], -1, 0]], 
   DateString /@ Extract[date, Position[closeN, closeN[[x]]]]}, {x, 1,
    Length[closeN]}], {y, 1, Length[priceRange]}]

What it basically does is looking when a price is greater (assigns a 1), smaller (a -1) or in between (0) a priceRange. It also extracts the date of the prices from closeN.

I always do it in this way, but I realize it might not be an optimal and fast way.

Any ideas or assumptions how to optimize the evaluation time?

here is a small subset:

date={{2000, 2, 2, 0, 0, 0.}, {2000, 2, 3, 0, 0, 0.}, {2000, 2, 4, 0, 0, 
      0.}, {2000, 2, 7, 0, 0, 0.}, {2000, 2, 8, 0, 0, 0.}, {2000, 2, 9, 0,
       0, 0.}, {2000, 2, 10, 0, 0, 0.}, {2000, 2, 11, 0, 0, 0.}, {2000, 2,
       14, 0, 0, 0.}, {2000, 2, 15, 0, 0, 0.}}

   closeN= {0.9766, 0.9899, 0.9838, 0.981, 0.9861, 0.9933, 0.9852, 0.9808,
    0.9791, 0.9816}

priceRange={{0.8273}, {0.8301}, {0.8394, 0.8354, 0.8363, 0.8393, 0.8383, 
  0.8396}, {0.8437, 0.8425, 0.8401, 0.844, 0.84, 0.8449, 0.8444, 
  0.8445}, {0.8494, 0.8491, 0.8485, 0.845, 0.8483, 0.8463, 0.8468, 
  0.8499, 0.8478, 0.8474, 0.8466, 0.8483, 0.8489}, {0.8532, 0.8546, 
  0.8517, 0.8507, 0.8545, 0.8541, 0.851, 0.8527, 0.8549, 0.8517, 
  0.8511, 0.8534, 0.8541, 0.8541, 0.8528}, {0.8587, 0.857, 0.8585, 
  0.8557, 0.8579, 0.8567, 0.8579, 0.8561, 0.8575, 0.8558, 0.8573, 
  0.8561, 0.8555, 0.8554, 0.8557, 0.855, 0.8581, 0.8596, 0.8598, 
  0.8559, 0.8582, 0.855, 0.8596, 0.8593}, {0.8639, 0.8601, 0.8646, 
  0.861, 0.863, 0.8612, 0.8635, 0.8608, 0.8611, 0.8617, 0.8606, 
  0.8631, 0.8608, 0.8608, 0.8636, 0.8618, 0.8622, 0.8621, 
  0.8638}, {0.867, 0.8695, 0.8687, 0.8685, 0.8655, 0.8677, 0.8662, 
  0.8657, 0.8695, 0.8652, 0.865, 0.8698, 0.8672, 0.8668, 0.8694, 
  0.8691, 0.866, 0.8693, 0.8655, 0.8699}, {0.8717, 0.8725, 0.8747, 
  0.8721, 0.8729, 0.8747, 0.8736, 0.8722, 0.8719, 0.8732, 0.8747, 
  0.8703, 0.8729, 0.871, 0.8742, 0.8736, 0.8706, 0.8704, 0.8717, 
  0.8747, 0.8726, 0.8713, 0.8723, 0.8747}}

here is whole the data (just copy it into mathematica and evaluate, then it should work):

closeN: https://dl.dropboxusercontent.com/u/19921198/closeN.txt

date: https://dl.dropboxusercontent.com/u/19921198/date.txt

priceRange: https://dl.dropboxusercontent.com/u/19921198/priceRange.txt

share|improve this question
    
forgot, sorry..see above –  holistic Apr 22 at 15:12
1  
Having a small data subset in the question would be so much more comfortable than having to browse three files. A self-contained question will also be more robust with regard to changing/removed links. –  Yves Klett Apr 22 at 15:27
    
ok no problem :)..edited it. But with this small subset it evaluates pretty fast –  holistic Apr 22 at 16:27
add comment

1 Answer 1

up vote 4 down vote accepted

DateString can be a slowpoke without an explicit output element specification. Also, you can apply it to your list of dates before you put them through your evaluation. Finally, you could also replace your Table expressions (and that Position/Extract usage) with Map and MapThread, along with some other small optimizations:

Here's an example modification run against your full dataset:

In[99]:= AbsoluteTiming[
 dateStrs =
  DateString[#, {"Year", "-", "Month", "-", "Day"}] & /@ date;
 result =
  With[{pMin = Min@#, pMax = Max@#},
     MapThread[
      Function[{c, d},
       {Piecewise@{{-1, c < pMin}, {1, c > pMax}}, d}
       ],
      {closeN, dateStrs}
      ]
     ] & /@ priceRange;
 ]

Out[99]= {1.611161, Null}

Compare to the original:

In[100]:= AbsoluteTiming[
 result =
   Table[
    Table[
     {If[closeN[[x]] > Max@priceRange[[y]],
       1,
       If[closeN[[x]] < Min@priceRange[[y]],
        -1,
        0
        ]
       ],
      DateString /@
       Extract[date,
        Position[closeN, closeN[[x]]]
        ]},
     {x, 1, Length[closeN]}
     ],
    {y, 1, Length[priceRange]}
    ];
 ]

Out[100]= {513.685032, Null}

The pre-application of DateString to your list of date lists with an explicit output element spec seems to be the major performance booster here. Keeping your program style functional and controlling evaluation well always helps, too.

Small notes: (1) The changes I made give you a differently formatted date string, but you can change that to whatever you want with the output element specification; (2) The changes I made give you a date string that is not enclosed in braces by itself. If you want the date enclosed in braces, you can change d to {d} in the body of the inner Function.

share|improve this answer
    
Thank you!It's quite a different way, trying to understand it now :). With my computer it takes 6 seconds to evaluate ;). –  holistic Apr 22 at 21:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.