# More efficient comparing

**URL:** <https://www.ruby-forum.com/t/more-efficient-comparing/129831>\
**Category:** Ruby\
**Created:** [February 21, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/more-efficient-comparing/129831 "2008-02-21T23:24:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@kyleh](https://www.ruby-forum.com/u/kyleh)\
**Post date:** [February 21, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/more-efficient-comparing/129831/1 "2008-02-21T23:24:41Z")

</div>

Hello,

I’ve got an array that holds urls. Example Element:  
[http://bla.random.com/bla.jpg](http://bla.random.com/bla.jpg).

I also have images in a directory. I’d like my script to compare the  
bla.jpg from the URL with all files in a directory to make sure it’s not  
a duplicate of something that’s already there - if it is - delete it  
from the array. My current way of doing it uses quite a bit of  
resources, was wondering if someone could show me a more efficient  
example if possible.

Current Code:  
Dir[“#{$baseDir}/#{board}/#{$dateString}/\*\*”].each do |file|  
$imgArray.delete\_if{  
|i| i =~ /#{file.split(‘/’).pop}/  
}

---

<div class="post-metadata">

**Author:** ![kyleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@kyleh](https://www.ruby-forum.com/u/kyleh)\
**Post date:** [February 21, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/more-efficient-comparing/129831/2 "2008-02-21T23:49:19Z")

</div>

On Thu, Feb 21, 2008 at 11:24 PM, Kyle H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> example if possible.
> 
> Current Code:  
> Dir[“#{$baseDir}/#{board}/#{$dateString}/\*\*”].each do |file|  
> $imgArray.delete\_if{  
> |i| i =~ /#{file.split(‘/’).pop}/  
> }

1. use file.split(‘/’).last instead of pop - pop modifies the array

2. move the regex out of the block - you’ll save some object  
constructions/destructions, and it’ll be easier on GC too.

3. try instead of splitting using a regex or rindex.  
i.e. last\_part = $1 if file =~ //([^/]\*)$/  
or last\_part = file[file.rindex(‘/’)…-1] or  
file[(file.rindex(‘/’)||0)…-1] to fix the cae when there’s no ‘/’ in  
the filename.

4. use Benchmark class to measure your improvements - that way, you’ll  
know exactly if the new code is better or not.

---

<div class="post-metadata">

**Author:** ![kyleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@kyleh](https://www.ruby-forum.com/u/kyleh)\
**Post date:** [February 21, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/more-efficient-comparing/129831/3 "2008-02-21T23:59:54Z")

</div>

Kyle H. wrote:

> example if possible.
> 
> Current Code:  
> Dir["#{$baseDir}/#{board}/#{$dateString}/\*\*"].each do |file|  
> $imgArray.delete\_if{  
> |i| i =~ /#{file.split(’/’).pop}/  
> }

Try:

require ‘set’

files = Dir["#{$baseDir}/#{board}/#{$dateString}/\*\*"].map { |file|  
File.basename(file) }.to\_set

$imgArray.delete\_if {|i| files.include? i.split("/")[-1] }

-Justin

---

<div class="post-metadata">

**Author:** ![kyleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@kyleh](https://www.ruby-forum.com/u/kyleh)\
**Post date:** [February 22, 2008, 12:14am UTC](https://www.ruby-forum.com/t/more-efficient-comparing/129831/4 "2008-02-22T00:14:14Z")

</div>

Thanks both of you,

Justin’s example seems to have made it more efficient. Later on I’ll  
compare my original, a version with Jano’s suggestions, and Justin’s  
using benchmark to see what is the most efficient.

Any more suggestions are welcome, of course, the more to benchmark the  
merrier!
