her.esy.fun/src/Scratch/fr/blog/2010-02-23-When-regexp-is-not-the-best-solution/index.html
Yann Esposito (Yogsototh) 059fabd7d0
many minor details to update
2022-10-26 11:38:50 +02:00

127 lines
7.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>YBlog - Quand se passer des expressions régulières ?</title>
<meta name="keywords" content="programmation, regexp, expressions régulières, extension, fichier" />
<link rel="shortcut icon" type="image/x-icon" href="../../../../Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="../../../../css/y.css" />
<link rel="stylesheet" type="text/css" href="/css/legacy.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" href="../../../../Scratch/img/about/FlatAvatar@2x.png" />
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<!-- IndieAuth -->
<link href="https://twitter.com/yogsototh" rel="me">
<link href="https://github.com/yogsototh" rel="me">
<link href="mailto:yann.esposito@gmail.com" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
</head>
<body lang="fr" class="article">
<div id="content">
<div id="header">
<div id="choix">
<span id="choixlang">
<a href="../../../../Scratch/en/blog/2010-02-23-When-regexp-is-not-the-best-solution/">Anglais</a>
</span>
<span class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
</div>
</div>
<div id="titre">
<h1>Quand se passer des expressions régulières ?</h1>
</div>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<p>Les expressions régulières sont très utiles. Cependant, elles ne sont pas toujours la meilleure manière daborder certain problème autour des chaines de caractères. Et surtout quand les transformations que vous voulez accomplir sont simples.</p>
<p>Je voulais savoir comment récupérer le plus vite possible lextension dun nom de fichier. Il y a trois manière naturelle daccomplir celà&nbsp;:</p>
<div>
<p><code class="ruby"> # regexp str.match(/[^.]*<span class="math inline">/);<em>e</em><em>x</em><em>t</em>=</span>&amp;</p>
<h1 id="split">split</h1>
<p>ext=str.split(.)[-1]</p>
<h1 id="file-module">File module</h1>
ext=File.extname(str) </code>
</div>
<p>A première vue, je pensais que lexpression régulière serait plus rapide que le <code>split</code> parce quil pouvait y avoir plusieurs de <code>.</code> dans un nom de fichier. Mais la majorité du temps il ny a quun seul point par nom de fichier. Cest pourquoi jai réalisé que le <code>split</code> serait plus rapide. Mais pas le plus rapide possible. Il y a une fonction qui est dédiée à faire ce travail dans un module standard de ruby ; le module <code>File</code>.</p>
<p>Voici le code pour faire un benchmark&nbsp;:</p>
<div>
<p><code class="ruby" file="regex_benchmark_ext.rb"> #!/usr/bin/env ruby require benchmark n=80000 tab=[ /accounts/user.json, /accounts/user.xml, /user/titi/blog/toto.json, /user/titi/blog/toto.xml ]</p>
puts “Get extname” Benchmark.bm do |x| x.report(“regexp:”) { n.times do str=tab[rand(4)]; str.match(/[^.]*<span class="math inline">/);<em>e</em><em>x</em><em>t</em>=</span>&amp;; end } x.report(" split:“) { n.times do str=tab[rand(4)]; ext=str.split(.)[-1] ; end } x.report(” File:") { n.times do str=tab[rand(4)]; ext=File.extname(str); end } end </code>
</div>
<p>Et voici les résultats&nbsp;:</p>
<pre class="twilight">
Get extname
user system total real
regexp: 2.550000 0.020000 2.570000 ( 2.693407)
split: 1.080000 0.050000 1.130000 ( 1.190408)
File: 0.640000 0.030000 0.670000 ( 0.717748)
</pre>
<p>En conclusion, les fonction dédiées sont meilleures que votre façon de faire (la plupart du temps).</p>
<h2 id="chemin-complet-dun-fichier-sans-lextension">Chemin complet dun fichier sans lextension</h2>
<div>
<p><code class="ruby" file="regex_benchmark_strip.rb"> #!/usr/bin/env ruby require benchmark n=80000 tab=[ /accounts/user.json, /accounts/user.xml, /user/titi/blog/toto.json, /user/titi/blog/toto.xml ]</p>
puts “remove extension” Benchmark.bm do |x| x.report(" File:“) { n.times do str=tab[rand(4)]; path=File.expand_path(str,File.basename(str,File.extname(str))); end } x.report(”chomp:") { n.times do str=tab[rand(4)]; ext=File.extname(str); path=str.chomp(ext); end } end </code>
</div>
<p>et voici les résultats&nbsp;:</p>
<pre class="twilight">
remove extension
user system total real
File: 0.970000 0.060000 1.030000 ( 1.081398)
chomp: 0.820000 0.040000 0.860000 ( 0.947432)
</pre>
<p>En conclusion du ce second benchmark. Un fonction simple est meilleure que trois fonctions dédiées. Pas de surprise, mais cest toujours bien de savoir.</p>
</div>
<div id="afterarticle">
<div id="social">
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
·
<a href="https://twitter.com/home?status=http%3A%2F%2Fyannesposito.com/Scratch/fr/blog/2010-02-23-When-regexp-is-not-the-best-solution/%20via%20@yogsototh" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
·
<a href="http://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fyannesposito.com/Scratch/fr/blog/2010-02-23-When-regexp-is-not-the-best-solution/" target="_blank" rel="noopener noreferrer nofollow" class="social">FB</a>
<br />
<a class="message" href="../../../../Scratch/fr/blog/Social-link-the-right-way/">Ces liens sociaux préservent votre vie privée</a>
</div>
<div id="navigation">
<a href="../../../../">Accueil</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/fr/blog">Blog</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/fr/softwares">Logiciels</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/fr/about">Auteur</a>
</div>
<div id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
<div>
Published on 2010-02-23
</div>
<div>
<a href="https://twitter.com/yogsototh">Follow @yogsototh</a>
</div>
<div>
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">Yann Esposito©</a>
</div>
<div>
Done with
<a href="http://www.vim.org" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="http://spacemacs.org" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="http://nanoc.ws" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="http://jaspervdj.be/hakyll" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>