RE-Modul, die Entdeckung der Langsamkeit?
Verfasst: Montag 20. Oktober 2008, 16:58
Moin,
will hier nicht provozieren, denn das RE-Modul ist eine wirklich tolle Sache, aber im 1:1 Vergleich mit der Zufuss-Methode mit der Stringfunktion "find", scheint es nicht gerade das Schnellste zu sein.
Ich denke mal, dass ich da was falsch gemacht habe, denn bei 50000 Durchläufen ist es bereits halb so schnell wie die Zufussmethode und bei 100000 Durchläufen sogar nur noch 1/3 so schnell. Hier mein Testscript:
Gruß, Seven
will hier nicht provozieren, denn das RE-Modul ist eine wirklich tolle Sache, aber im 1:1 Vergleich mit der Zufuss-Methode mit der Stringfunktion "find", scheint es nicht gerade das Schnellste zu sein.
Ich denke mal, dass ich da was falsch gemacht habe, denn bei 50000 Durchläufen ist es bereits halb so schnell wie die Zufussmethode und bei 100000 Durchläufen sogar nur noch 1/3 so schnell. Hier mein Testscript:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import re, urllib2
from httplib import BadStatusLine
from time import localtime
def scansource1():
# duchsucht mit Hilfe des RE-Moduls eine Website nach Informationen
expression1 = re.compile("www.myspace.com/([a-z]*)\s*</title>")
expression2 = re.compile('width="175".*break-word">(.*)</td></tr>\s*</table>')
expression3 = re.compile('<span class="nametext">(.*)</span><br>')
expression4 = re.compile("</strong></font>\s*<br>\s*<br>\s*.*\s*<br>\s*(.*)")
expression5 = re.compile('SWFObject\(\"http://musicservices.myspace.com/Modules/MusicServices/Services/Embed.ashx/ptype=4(.*)", "shell')
url="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=40254287"
online = False
try:
u = urllib2.urlopen(url)
except (urllib2.URLError, BadStatusLine):
pass
else:
online = True
buffer = u.read()
try:
source = unicode(buffer, 'iso-8859-15','replace')
except:
source = buffer
if online:
src1 = source[:60000]
src2 = source[180000:190000]
print localtime()[4:8]
for i in range(50000):
# Adresse
address = str(re.findall(expression1, src1))[3:-2]
if address == '': address = 'invalid address'
# Label
label = str(re.findall(expression2, src1))[3:-2]
if label == '': label = 'no label found'
# Name
name = str(re.findall(expression3, src1))[3:-2]
if name == '': name = 'no name found'
# Land
country = str(re.findall(expression4, src1))[3:-3]
if country == []: country = 'no country found'
# Playerurl
playerurl = str(re.findall(expression5 , src2))[9:-2] # without ap=1,
if playerurl == '': playerurl = 'no player'
print localtime()[4:8]
print
def scansource2():
# hangelt sich von Keyword zu Keyword durch den Seitencode und sammelt Informationen
url="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=40254287"
online = False
try:
u = urllib2.urlopen(url)
except (urllib2.URLError, BadStatusLine):
pass
else:
online = True
buffer = u.read()
try:
source = unicode(buffer, 'iso-8859-15','replace')
except:
source = buffer
if online:
print localtime()[4:8]
for i in range(50000):
# Adresse
pos1 = source.find('www.myspace.com',0)
pos2 = source.find('</title>',pos1)
address= source[pos1+16:pos2-2]
tst = re.findall("[A-Z!$%&/()=?\{}*+#',.;:_<>|^-]",address)
if len(tst) != 0: address = 'invalid address'
# Name
pos1 = source.find('"nametext"',0)
if pos1 > 0:
pos2 = source.find('</span><br>',pos1)
name = source[pos1+11:pos2].strip()
else:
name = 'no name found'
# Land
pos1=source.find('user.viewAlbums&friendID=',pos2)
if pos1 > 0:
pos2 = source.find('</strong></font>',pos1)
if pos2 > 0:
pos3 = source.find('<br>',pos2)
if pos3 > 0:
pos4 = source.find('<br>',pos3+1)
if pos4 > 0:
pos5 = source.find('<br>',pos4+1)
if pos5 > 0:
pos6 = source.find('<br>',pos5+1)
if pos6 > 0:
country = source[pos5+5 : pos6].strip()
# Label
label = 'no label found'
pos0 = 0
pos1 = source.find('"lightbluetext8"',pos0)
if pos1 > 0:
pos2 = source.find('</table>',pos1)
if pos2 > 0:
pos3 = source.find('</td></tr>',pos2-40)
if pos3 > 0:
pos4 = source.find('break-word">',pos3-20)
if pos4 > 0:
label = source[pos4+12:pos3]
# Playerurl
pos1 = source.find('SWFObject("')
if pos1 > 0:
pos2 = source.find('", "',pos1)
if pos2 > 0:
playerurl = source[pos1+11:pos2]
playerurl = playerurl.replace('http://musicservices.myspace.com/Modules/MusicServices/Services/Embed.ashx/ptype=4,ap=1,','')
playerurl = playerurl.replace('http://musicservices.myspace.com/Modules/MusicServices/Services/Embed.ashx/ptype=4,ap=0,','')
print localtime()[4:8]
print '------------'
scansource1()
scansource2()