# More efficient comparing

**URL:** <https://rubytalk.org/t/more-efficient-comparing/44684>\
**Category:** ruby-talk\
**Created:** [21 February 2008 22:24 UTC](https://rubytalk.org/t/more-efficient-comparing/44684 "2008-02-21T22:24:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle\_Hunter](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyle\_Hunter](https://rubytalk.org/u/Kyle_Hunter)\
**Post date:** [21 February 2008 22:24 UTC](https://rubytalk.org/t/more-efficient-comparing/44684/1 "2008-02-21T22:24:39Z")

</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|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$imgArray.delete\_if{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>i\> i =~ /#{file.split('/').pop}/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [21 February 2008 22:47 UTC](https://rubytalk.org/t/more-efficient-comparing/44684/2 "2008-02-21T22:47:39Z")

</div>

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.

> **···**
>
> On Thu, Feb 21, 2008 at 11:24 PM, Kyle Hunter \<keletmaster@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I've got an array that holds urls. Example Element:  
> > [http://bla.random.com/bla.jpg\](http://bla.random.com/bla.jpg%5C).
> > 
> > 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|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$imgArray.delete\_if{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>i\> i =~ /#{file.split('/').pop}/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [21 February 2008 22:58 UTC](https://rubytalk.org/t/more-efficient-comparing/44684/3 "2008-02-21T22:58:59Z")

</div>

Kyle Hunter wrote:

> Hello,
> 
> I've got an array that holds urls. Example Element:  
> [http://bla.random.com/bla.jpg\](http://bla.random.com/bla.jpg%5C).
> 
> 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|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$imgArray.delete\_if{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>i\> i =~ /#{file.split('/').pop}/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;  
> 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:** ![Kyle\_Hunter](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyle\_Hunter](https://rubytalk.org/u/Kyle_Hunter)\
**Post date:** [21 February 2008 23:14 UTC](https://rubytalk.org/t/more-efficient-comparing/44684/4 "2008-02-21T23:14:07Z")

</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!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
