Reputation: 1082
My code is here
str= "In 2004, Obama received national attention during his campaign to represent Illinois in the United States Senate"
arr =str.scan(/\S+(?:\s+\S+)?/)
it gives
arr=["In 2004,", "Obama received", "national attention", "during his", "campaign to", "represent Illinois", "in the", "United States", "Senate"]
fresh_arr=[]
arr.each do |el|
if !arr.match(/is|am|are|this|his/)
fresh_arr << el
end
end
Now i want to remove that element which has contain (is,am,are,this,his) type of string then result something like this
arr=["Obama received", "national attention","represent Illinois","United States", "Senate"]
I have very big data and it will take 6 sec can i do this any another way
Upvotes: 0
Views: 90
Reputation: 10137
Simple way to do it. But I don't know about performance. Because map
still runs the loop which you are running.
arr.map{|x| x unless x =~ /\b(in|am|are|his|this)\b/i}.compact
Benchmark:
> my_bm(500000){arr.map{|x| x unless x =~ /\b(in|am|are|his|this)\b/i}.compact}
user system total real
7.430000 0.000000 7.430000 ( 7.451064)
=> nil
> my_bm(500000){arr.reject! { |e| e =~ /\b(in|am|are|his|this)\b/i }}
user system total real
4.620000 0.000000 4.620000 ( 4.623782)
> my_bm(5000000){arr.map{|x| x unless x =~ /\b(in|am|are|his|this)\b/i}.compact}
user system total real
50.790000 0.010000 50.800000 ( 50.840533)
> my_bm(5000000){arr.reject! { |e| e =~ /\b(in|am|are|his|this)\b/i }}
user system total real
46.140000 0.010000 46.150000 ( 46.198752)
=> nil
Upvotes: 2