PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > More efficient comparing
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
More efficient comparing

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 22h24   #1
Kyle Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut More efficient comparing

Hello,

I've got an array that holds urls. Example Element:
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}/
}
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/02/2008, 22h47   #2
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: More efficient comparing

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.
>
> 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}/
> }


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.

  Réponse avec citation
Vieux 21/02/2008, 22h58   #3
Justin Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: More efficient comparing

Kyle Hunter wrote:
> Hello,
>
> I've got an array that holds urls. Example Element:
> 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}/
> }
>


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


  Réponse avec citation
Vieux 21/02/2008, 23h14   #4
Kyle Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: More efficient comparing

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11013 seconds with 12 queries