julien2424
Messages postés76Date d'inscriptionmardi 8 août 2006StatutMembreDernière intervention24 avril 2012
-
24 juin 2011 à 18:46
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 2011
-
25 juin 2011 à 23:30
Bonjour à tous
Je reviens car je me pose des questions sur un code que j'utilise.
A force de l'utiliser, je n'arrive plus à voir comme l'optimiser et j'aimerai qu'on m'aide avec un regard neuf car son utilisation nécessite 600mo de ressources et l'utilisation à 100% du processeur.
Pour information c'est un code que j'ai pris sur internet et que j'ai améliorer.
Le voici:
Si vous avez un peu de temps à perdre pour m'aider.
PS: Je n'ai pas coller ici les constantes qui permettent de mettre en place les balises START / STOP puisqu'elles sont sans intérêt dans une optique d'optimisation.
Private Function Dir_NotBin(ByVal CurDir As String) As String
If Microsoft.VisualBasic.Right(CurDir, 4) = "\bin" Then
Return Mid(CurDir, 1, Len(CurDir) - 4)
Else
Return CurDir
End If
End Function
Private Function HTML2CHAR(ByVal S As String) As String
Dim P1 As Integer = -1
Dim P9 As Integer
Dim V As Integer
'
While P1 <> 0
P1 = InStr(S, "&#")
If P1 > 0 Then
P9 = InStr(P1, S, ";")
V = Val(Mid(S, P1 + 2, P9 - P1))
S = Mid(S, 1, P1 - 1) & Chr(V) & Mid(S, P9 + 1)
End If
End While
Return S
End Function
Dim DNL_FILE As String = Nothing
Public Structure WX_DATA_STRUC
Dim LOCATION As String
Dim TIME_OBS As String
Dim ICON As String
Dim CONDITIONS As String
Dim TEMPERATURE As String
Dim CHILL As String
Dim PRESSURE As String
Dim PRESS_TEND As String
Dim PRESS_ICON As String
Dim DEW_POINT As String
Dim HUMIDITY As String
Dim VISIBILITY As String
Dim WIND_SPD As String
Dim WIND_DIR As String
Dim SUNRISE As String
Dim SUNSET As String
Dim PRECIP As String
Dim PRECIP1h As String
Dim UV As String
Dim TEMP_HAUT As String
Dim TEMP_BASSE As String
End Structure
Dim WX_DATA As WX_DATA_STRUC
Private Sub EXEC_JOB()
DNLD_PAGE()
ANLZ_PAGE()
DSPL_DATA()
End Sub
Private Sub DNLD_PAGE()
Dim F As Integer = FreeFile()
Dim WEB_CLIENT As New WebClient
Dim DATA_BUFFER As Byte()
Dim DOWNLOAD_DATA As String
Dim TRANS_DATA As String
Dim I As Integer = 0
Dim X As String = Nothing
Dim Path1 As String = GetAppData()
Directory.CreateDirectory(Path1)
Dim path As String = (Path1)
Dim SRC_FILE As String = path & "\meteo"
DNL_FILE = SRC_FILE & ".temporaire" & ".smw"
Try
DATA_BUFFER = WEB_CLIENT.DownloadData(My.Settings.Lien_Ville)
DOWNLOAD_DATA = System.Text.Encoding.UTF8.GetString(DATA_BUFFER)
X = ""
For I = 0 To DOWNLOAD_DATA.Length - 1
X += DOWNLOAD_DATA.Chars(I)
Next
TRANS_DATA = HTML2CHAR(X)
FileOpen(F, DNL_FILE, OpenMode.Output, OpenAccess.Write)
Print(F, X)
FileClose(F)
FileOpen(F, SRC_FILE & ".smw", OpenMode.Output, OpenAccess.Write)
Print(F, TRANS_DATA)
FileClose(F)
Catch ex As Exception
'MsgBox(REMOTE_URL & vbCrLf & ex.Message)
'Me.Close()
End Try
End Sub
Private Sub ANLZ_PAGE()
Dim F As Integer = FreeFile()
Dim EOI As Boolean = False
Dim WRK As Boolean = False
'
Dim P, P1, P2 As Integer
Dim X As String
Dim S As String = Nothing
Dim Z As String
Try
FileOpen(F, DNL_FILE, OpenMode.Input, OpenAccess.Read)
Catch ex As Exception
End Try
Try
While Not EOI
If EOF(F) Then
EOI = True
Else
X = LineInput(F)
fenetre1.TEMPORAIRE.Text = X
S = fenetre1.TEMPORAIRE.Text
P = InStr(S, BALISE_LOCATION_START)
If P > 0 Then
S = Mid(S, P + BALISE_LOCATION_START.Length)
EOI = True
WRK = True
End If
End If
End While
Catch ex As Exception
My.Settings.JMC_SM_Connexion_internet_défaillante = True
End Try
FileClose(F)
Try
If WRK Then
Dim P0 As Integer
'/***** NOYAU DU TRAITEMENT *****/
'
'P = InStr(S, BALISE_LOCATION_STOP)
'WX_DATA.LOCATION = Mid(S, 1, P - 1)
'P = InStr(S, BALISE_WXCONDITIONS_START)
'S = Mid(S, P + BALISE_WXCONDITIONS_START.Length)
Dim S1 As String
S1 = S
P = InStr(S, BALISE_TIME_OBS_START)
S = Mid(S, P + BALISE_TIME_OBS_START.Length)
WX_DATA.TIME_OBS = Mid(S, 1, InStr(S, BALISE_TIME_OBS_STOP) - 1)
P = InStr(S, BALISE_ICON_START)
S = Mid(S, P + BALISE_ICON_START.Length)
WX_DATA.ICON = Mid(S, 1, InStr(S, BALISE_ICON_STOP) - 1)
P = InStr(S, BALISE_CONDITIONS_START)
S = Mid(S, P + BALISE_CONDITIONS_START.Length)
WX_DATA.CONDITIONS = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_CONDITIONS_STOP) - 1))
P = InStr(S, BALISE_TEMPERATURE_START)
S = Mid(S, P + BALISE_TEMPERATURE_START.Length)
WX_DATA.TEMPERATURE = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_TEMPERATURE_STOP) - 1))
P = InStr(S, BALISE_CHILL_START)
S = Mid(S, P + BALISE_CHILL_START.Length)
WX_DATA.CHILL = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_CHILL_STOP) - 1))
P = InStr(S, BALISE_PRESSURE_START)
S = Mid(S, P + BALISE_PRESSURE_START.Length)
P1 = InStr(S, BALISE_PRESSURE_STOP1)
P2 = InStr(S, BALISE_PRESSURE_STOP2)
If P2 = 0 Then ' pas d'indication de tendance barométrique
WX_DATA.PRESSURE = Mid(S, 1, InStr(S, BALISE_PRESSURE_STOP1) - 1)
Else
WX_DATA.PRESSURE = Mid(S, 1, InStr(S, BALISE_PRESSURE_STOP2) - 1)
P0 = InStr(S, "../pt")
If P0 > 0 Then
WX_DATA.PRESS_ICON = Mid(S, P0 + 3, 9)
P0 = InStr(WX_DATA.PRESS_ICON, ".gif")
WX_DATA.PRESS_ICON = Mid(WX_DATA.PRESS_ICON, 1, P0 + 3)
End If
'P = InStr(S, BALISE_PRESS_TEND_START)
'S = Mid(S, P + BALISE_PRESS_TEND_START.Length + 2)
'WX_DATA.PRESS_TEND = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_PRESS_TEND_STOP) - 1))
End If
P = InStr(S, BALISE_DEW_POINT_START)
S = Mid(S, P + BALISE_DEW_POINT_START.Length)
WX_DATA.DEW_POINT = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_DEW_POINT_STOP) - 1))
P = InStr(S, BALISE_HUMIDITY_START)
S = Mid(S, P + BALISE_HUMIDITY_START.Length)
WX_DATA.HUMIDITY = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_HUMIDITY_STOP) - 1))
P = InStr(S, BALISE_VISIBILITY_START)
S = Mid(S, P + BALISE_VISIBILITY_START.Length)
WX_DATA.VISIBILITY = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_VISIBILITY_STOP) - 1))
Z = S
P = InStr(S, BALISE_WIND_SPD_START)
S = Mid(S, P + BALISE_WIND_SPD_START.Length)
WX_DATA.WIND_SPD = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_WIND_SPD_STOP) + 3))
P = InStr(S, BALISE_WIND_DIR_START)
If P > 0 And P < 150 Then
S = Mid(S, P + BALISE_WIND_DIR_START.Length)
WX_DATA.WIND_DIR = HTML2CHAR(Mid(S, 1, InStr(S, BALISE_WIND_DIR_STOP) - 1))
Else
WX_DATA.WIND_DIR = ""
End If
P = InStr(Z, BALISE_SUNRISE_START)
Z = Mid(Z, P + BALISE_SUNRISE_START.Length)
WX_DATA.SUNRISE = Mid(Z, 1, InStr(Z, BALISE_SUNRISE_STOP) - 1)
P = InStr(Z, BALISE_SUNSET_START)
Z = Mid(Z, P + BALISE_SUNSET_START.Length)
WX_DATA.SUNSET = Mid(Z, 1, InStr(Z, BALISE_SUNSET_STOP) - 1)
P = InStr(S1, BALISE_PRECIP1h_START)
S1 = Mid(S1, P + BALISE_PRECIP1h_START.Length)
WX_DATA.PRECIP1h = Mid(S1, 1, InStr(S1, BALISE_PRECIP1h_STOP) - 1)
If WX_DATA.PRECIP1h = "NA" Then
WX_DATA.PRECIP1h = "0 mm"
End If
P = InStr(S1, BALISE_PRECIP_START)
S1 = Mid(S1, P + BALISE_PRECIP_START.Length)
WX_DATA.PRECIP = Mid(S1, 1, InStr(S1, BALISE_PRECIP_STOP) - 1)
P = InStr(S1, BALISE_UV_START)
S1 = Mid(S1, P + BALISE_UV_START.Length)
WX_DATA.UV = Mid(S1, 1, InStr(S1, BALISE_UV_STOP) - 1)
P = InStr(S1, BALISE_TEMP_HAUT_START)
S1 = Mid(S1, P + BALISE_TEMP_HAUT_START.Length)
WX_DATA.TEMP_HAUT = Mid(S1, 1, InStr(S1, BALISE_TEMP_HAUT_STOP) - 1)
P = InStr(S1, BALISE_TEMP_BASSE_START)
S1 = Mid(S1, P + BALISE_TEMP_BASSE_START.Length)
WX_DATA.TEMP_BASSE = Mid(S1, 1, InStr(S1, BALISE_TEMP_BASSE_STOP) - 1)
GC.Collect()
Else
End If
Catch ex As Exception
fenetre1.observation_heure.Text = "ND"
fenetre1.Conditions_actuelles.Text = "Indisponible"
fenetre1.Temperature_texte.Text = "ND"
fenetre1.température_resentie_résultat.Text = "ND"
fenetre1.pression_resultat.Text = "ND"
'fenetre1.pression_etat_resultat.Text = WX_DATA.PRESS_TEND
fenetre1.rosée_resultat.Text = "ND"
fenetre1.humidité_resultat.Text = "ND"
fenetre1.visibilité_resultat.Text = "ND"
fenetre1.vent_vitesse_resultat.Text = "ND"
fenetre1.vent_direction_resultat.Text = "ND"
fenetre1.levée_texte.Text = "ND"
fenetre1.couché_texte.Text = "ND"
fenetre1.precipitation_resultat.Text = "ND"
fenetre1.précipitation_dernière_heure.Text = "ND"
fenetre1.indiceUV_resultat.Text = "ND"
fenetre1.température_basse.Text = "ND"
fenetre1.température_haute.Text = "ND"
End Try
End Sub
Private Sub DSPL_DATA()
If My.Settings.JMC_SM_Connexion_internet_défaillante = False Then
WX_IMG_INDEX = Val(WX_DATA.ICON)
fenetre1.Icone_météo.Image = fenetre1.Icônes_temps.Images.Item(WX_IMG_INDEX)
fenetre1.observation_heure.Text = WX_DATA.TIME_OBS
fenetre1.Conditions_actuelles.Text = WX_DATA.CONDITIONS
fenetre1.Temperature_texte.Text = WX_DATA.TEMPERATURE
fenetre1.température_resentie_résultat.Text = WX_DATA.CHILL
fenetre1.pression_resultat.Text = WX_DATA.PRESSURE
fenetre1.rosée_resultat.Text = WX_DATA.DEW_POINT
fenetre1.humidité_resultat.Text = WX_DATA.HUMIDITY
fenetre1.visibilité_resultat.Text = WX_DATA.VISIBILITY
fenetre1.vent_vitesse_resultat.Text = WX_DATA.WIND_SPD
fenetre1.vent_direction_resultat.Text = WX_DATA.WIND_DIR
fenetre1.levée_texte.Text = WX_DATA.SUNRISE
fenetre1.couché_texte.Text = WX_DATA.SUNSET
fenetre1.precipitation_resultat.Text = WX_DATA.PRECIP
fenetre1.précipitation_dernière_heure.Text = WX_DATA.PRECIP1h
fenetre1.indiceUV_resultat.Text = WX_DATA.UV
fenetre1.température_basse.Text = WX_DATA.TEMP_BASSE
fenetre1.température_haute.Text = WX_DATA.TEMP_HAUT
GC.Collect()
End If
End Sub
Merci de votre aide à tous et à toutes,
Bonne soirée
The Meteorologist
Messages postés232Date d'inscriptionjeudi 18 janvier 2007StatutMembreDernière intervention 3 novembre 20111 24 juin 2011 à 19:20
Pas très joli tout ça
Pour commencer, je te suggère de réécrire ton code en utilisant correctement le VB.NET. En effet, tu utilises essentiellement les méthodes de VB6 ce qui est extrêmement déconseillé !
Ensuite essaie de renommer tes membres avec des noms explicites et de respecter la casse :
Tu fais tout cela, tu auras un code plus lisible et plus maintenable. Et je m'engagerai personnellement à t'aider à l'optimiser ensuite si tu le souhaites
julien2424
Messages postés76Date d'inscriptionmardi 8 août 2006StatutMembreDernière intervention24 avril 2012 24 juin 2011 à 19:28
Re
Et merci Simon de ton aide.
Alors comme tu t'en doute, j'ai récupérer ce code, l'ai adapté sans trop le comprendre moi non plus.
Je savais que les lignes de code été dépassés mais j'ignorais que c'était même pas du Vb.net.
Je vais essayez de revoir tout ça, je t'avoue que je suis déjà perdu car j'ai essayé de le moderniser avec des lignes vb.net, en vain.
On peut discuter de part ou je dois commencer si tu as un peu de temps à me consacrer :$
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 24 juin 2011 à 22:37
Salut,
Je suis d'accord avec les remarques de simon.
Reecrit toutes les fonction de manipulation de chaine de oaracteres en vb.net
avec des methode de la classe string
pas de mid, instr STP des substring des contains des replace, c'est plus rapide
et facile a lire.
Merci de ne pas mettre de GC.collect
C'est ce qui faut faire pour avoir des perfs de merde :-)
le GC est gourmand en travail pour liberer de la memoire
julien2424
Messages postés76Date d'inscriptionmardi 8 août 2006StatutMembreDernière intervention24 avril 2012 24 juin 2011 à 22:43
Merci pour ta réponse,
Je vais précicer à quoi sert le code:
Ce module charge une page web de ce type: http://meteo.msn.com/local.aspx?wealocations=wc:FRXX0016 et l’encode en UTF8 dans un document de style bloc note. (Il l'enregistre dans Roaming à ma demande.)
Malheureusement MSN (Le plus précis des services météo en ligne pour les villes) ne fourni pas de document XML ou RSS. Sinon le Framework aurais pu lire facilement les détails des informations avec des XMLReader.
Au final, le module doit trouver dans la page web chargée entièrement en ligne de code web, les éléments qui permettent de répondre aux informations météorologiques demandées.
Après tes remarques m'ont l'air pertinentes mais je ne saurais les appliquer :s
J'ai tout appris tout seul en développement, je bidouille plus que je ne suis pro... j'ai beaucoup de lacunes :s
Jules
Vous n’avez pas trouvé la réponse que vous recherchez ?
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 25 juin 2011 à 08:49
Ok,
Il faut essayer d'y aller petit a petit.
La modification n'est pas trop dur a effectuer.
Pour les regexp, il suffit de lire un tuto pour comprendre le principe
et de prendre un logiciel pour tester une regexp http://osherove.com/tools
julien2424
Messages postés76Date d'inscriptionmardi 8 août 2006StatutMembreDernière intervention24 avril 2012 25 juin 2011 à 09:11
Je commence à voir un peu plus clair dans mon code.
Les ressources bondisses dans un premier temps car une form1 chargé les éléments visuels d'une form2 (Une idée que j'avais eu pour aller plus vite, je viens de rectifier ça) et je dois préciser que mon appli charge prêt de 30 webbrowser. Quand je vois Firefox monter à 500mo pour 4onglets, l'explication est clair.
En revanche, le code dont je parlais est bel et bien long. Il met 6 à 7 secondes à se lancer, l'optimiser est donc essentiel.
Alors je me demande si c'est l'enregistrement du document en Freefile qui pose problème ou la recherche des éléments dans ce fameux document qui est longue?
Car ton idée des regexp à l'air bien sympa, mais il y'a beaucoup de répétitions dans les pages web MSN, et aucune précision comme on aurais pu l'attendre d'une XHTML.
Leur page est brouillonne et je me demande si les regexp seront vraiment efficace?
julien2424
Messages postés76Date d'inscriptionmardi 8 août 2006StatutMembreDernière intervention24 avril 2012 25 juin 2011 à 11:22
J'utilise cette méthode maintenant:
Cependant, elle semble assez lourde aussi:
Dim StartIndex As Integer = weather_data.IndexOf(BALISE_TIME_OBS_START)
Dim IndexEnd As Integer = weather_data.IndexOf(BALISE_TIME_OBS_STOP)
Dim temps_observations As String = weather_data.Substring(StartIndex + 10, IndexEnd - StartIndex - 10).Trim
fenetre1.observation_heure.Text = temps_observations
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 25 juin 2011 à 23:25
Non les webbrowser ne sont pas necessaires.
les données peuvent être chargées directement en http
(text et image en direct)
Le webbrowser est fait pour faire de l'affichage de page web.
pas besoin de telecharger les flash, pubs et autres fichier css, javascript, images
que le webbrowser a besoin pour afficher la page.
Je n'ai pas etudies ce composant, mais il est plus lourd que de travailler en HTML pur.