How can I write an efficient version of the following statement?

Cases[list, {_, feature_} /; feature < constant]; // AbsoluteTiming

List is of the form, list = {{feature11,feature12},...,{featuren1,featuren2}}. It has a length in the order of 10^5. On my laptop, a Macbook pro from last year it takes around 0.25s. However, I need to evaluate this expression a lot of times. Any suggestion. Thank you.

data = RandomReal[1, {500000, 2}];
constant = 0.3;
(ans1 = Cases[data, {_, feature_} /; feature < constant]); // AbsoluteTiming

{0.880057, Null}

(ans2 = Select[data, Last@# < constant &]); // AbsoluteTiming

{1.219078, Null}

(ans3 = Pick[data, Round[Last@# + (0.5 - constant)] & /@ data, 0]); // AbsoluteTiming

{0.110006, Null}

(ans4 = Pick[data, Round[data[[All, 2]] + (0.5 - constant)], 0]); // AbsoluteTiming

{0.045003, Null}

ans1 == ans2 == ans3 == ans4

True

  • For those data which have a larger range than {0,1}, using Round[(constant - data[[All, 2]])/10^n - .5] instead of Round[data[[All, 2]] + (0.5 - constant)] may be helpful. n can be any number to assure that Abs[(constant - data[[All, 2]])/10^n] < 0.5. – wuyingddg Sep 3 '14 at 15:26
  • 3
    Pick[data, Sign[data[[All, 2]] - constant], -1] – Coolwater Sep 3 '14 at 18:08

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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