canal visual basic .net

Recursos Visual Basic.NET, VB.NET, Manuales de programación, Tutoriales, Foros de programación, Comunidad de programadores

Usuarios activos:  85

Foros de programación, recursos, tutoriales, sistemas operativos...

Bienvenido a la zona de foros. Participa en alguno de nuestros foros: Foros de visual basic, foros de visual basic.net foros de Crystal reports, programas gratis, foros de C++ - C# , foros de Java, foros de PHP, foros de ASP.net. Seguro que hay un foro que te servirá de gran utilidad y si no lo encuentras avísanos y crearemos uno nuevo.
Resultados 1 al 5 de 5
  1. #1
    Vanchi está desconectado Moderador Veteran@
    Fecha de ingreso
    09 dic, 05
    Mensajes
    740

    Predeterminado

    ¿Las variables locales son más veloces que las globales y las públicas?

    Para averiguarlo veamos el siguiente ejemplo.

    Se debe copiar el código en un archivo de texto plano. Luego cambiarle la extensión a "frm".
    Luego hay que agregar un módulo con esta sóla instrucción:

    Código:
    1. Public pVariable%

    (Recordar cambiarle la extensión a ".frm")

    Código:
    1. VERSION 5.00
    2. Begin VB.Form Form1
    3.  BorderStyle = 1 'Fixed Single
    4. Caption = "Form1"
    5.  ClientHeight = 3465
    6.  ClientLeft = 45
    7.  ClientTop = 330
    8.  ClientWidth = 5520
    9.  LinkTopic = "Form1"
    10.  MaxButton = 0 'False
    11. MinButton = 0 'False
    12. ScaleHeight = 3465
    13.  ScaleWidth = 5520
    14.  StartUpPosition = 3 'Windows Default
    15. Begin VB.CommandButton cmdClear
    16.  Caption = "Borrar listas"
    17.  Height = 255
    18.    Left = 3720
    19.  TabIndex = 11
    20.     Top = 120
    21.   Width = 1695
    22.  End
    23.  Begin VB.TextBox txtRep
    24.  Height = 285
    25.    Left = 2160
    26.  TabIndex = 10
    27.    Text = "Text1"
    28.     Top = 120
    29.   Width = 1455
    30.  End
    31.  Begin VB.ListBox lstTiempos
    32.  Height = 2010
    33.   Index = 2
    34.    Left = 3720
    35.  TabIndex = 7
    36.     Top = 960
    37.   Width = 1695
    38.  End
    39.  Begin VB.CommandButton cmdOpciones
    40.  Caption = "Locales"
    41.  Height = 375
    42.   Index = 2
    43.    Left = 3720
    44.  TabIndex = 6
    45.     Top = 480
    46.   Width = 1695
    47.  End
    48.  Begin VB.ListBox lstTiempos
    49.  Height = 2010
    50.   Index = 1
    51.    Left = 1920
    52.  TabIndex = 4
    53.      Top&am p;nb sp; = 960
    54.   Width = 1695
    55.  End
    56.  Begin VB.CommandButton cmdOpciones
    57.  Caption = "Globales"
    58.  Height = 375
    59.   Index = 1
    60.    Left = 1920
    61.  TabIndex = 3
    62.     Top = 480
    63.   Width = 1695
    64.  End
    65.  Begin VB.ListBox lstTiempos
    66.  Height = 2010
    67.   Index = 0
    68.    Left = 120
    69.  TabIndex = 1
    70.     Top = 960
    71.   Width = 1695
    72.  End
    73.  Begin VB.CommandButton cmdOpciones
    74.  Caption = "Publicas"
    75.  Height = 375
    76.   Index = 0
    77.    Left = 120
    78.  TabIndex = 0
    79.     Top = 480
    80.   Width = 1695
    81.  End
    82.  Begin VB.Label Label2
    83.  BorderStyle = 1 'Fixed Single
    84. Caption = "Número de repeticiones:"
    85.  Height = 285
    86.    Left = 120
    87.  TabIndex = 9
    88.     Top = 120
    89.   Width = 2055
    90.  End
    91.  Begin VB.Label lblPromedio
    92.  BorderStyle = 1 'Fixed Single
    93. Caption = "Promedio:"
    94.  Height = 255
    95.   Index = 2
    96.    Left = 3720
    97.  TabIndex = 8
    98.     Top = 3120
    99.   Width = 1695
    100.  End
    101.  Begin VB.Label lblPromedio
    102.  BorderStyle = 1 'Fixed Single
    103. Caption = "Promedio:"
    104.  Height = 255
    105.   Index = 1
    106.    Left = 1920
    107.  TabIndex = 5
    108.    Top = 3120
    109.   Width = 1695
    110.  End
    111.  Begin VB.Label lblPromedio
    112.  BorderStyle = 1 'Fixed Single
    113. Caption = "Promedio:"
    114.  Height = 255
    115.   Index = 0
    116.   Left = 120
    117.  TabIndex = 2
    118.    Top = 3120
    119.   Width = 1695
    120.  End
    121. End
    122. Attribute VB_Name = "Form1"
    123. Attribute VB_GlobalNameSpace = False
    124. Attribute VB_Creatable = False
    125. Attribute VB_PredeclaredId = True
    126. Attribute VB_Exposed = False
    127. Option Explicit
    128. Dim TpoTotal&
    129. Dim gVariable%
    130.  
    131. Private Sub cmdClear_Click()
    132. Dim i%
    133. For i = 0 To 2
    134.  lstTiempos(i).Clear
    135. Next
    136. End Sub
    137.  
    138. Private Sub cmdOpciones_Click(Index As Integer)
    139. Dim Tpo! 'Para contar el tiempo
    140. Dim i&, j& 'Para el for
    141. Dim lVariable%
    142.  
    143. MousePointer = 11
    144. Tpo = Timer
    145. Select Case Index
    146.  Case 0 'Publica
    147. For i = 0 To TpoTotal
    148.  For j = 0 To 1000
    149.    If pVariable Then pVariable = pVariable
    150.  Next
    151.  Next
    152.  Case 1 'Global
    153. For i = 0 To TpoTotal
    154.  For j = 0 To 1000
    155.    If gVariable Then gVariable = gVariable
    156.  Next
    157.  Next
    158.  Case 2 'Local
    159. For i = 0 To TpoTotal
    160.  For j = 0 To 1000
    161.    If lVariable Then lVariable = lVariable
    162.  Next
    163.  Next
    164. End Select
    165. Tpo = Timer - Tpo
    166. MousePointer = 0
    167.  
    168. If Tpo < 0.1 Then
    169.  MsgBox "El resultado no es confiable y está afectado de errores." & vbNewLine & "Se aumentó el número repeticiones del bucle", vbExclamation
    170.  If Tpo < 0 Then
    171.  txtRep = TpoTotal * 100
    172.  Else
    173.  txtRep = CLng(TpoTotal * 0.5 / Tpo)
    174.  End If
    175.  cmdClear_Click
    176.  Exit Sub
    177. End If
    178.  
    179. lstTiempos(Index).AddItem Tpo
    180. Tpo = 0
    181. For i = 0 To lstTiempos(Index).ListCount - 1
    182.  Tpo = Tpo + lstTiempos(Index).List(i)
    183. Next
    184. lblPromedio(Index) = "Promedio: " & Tpo / lstTiempos(Index).ListCount
    185. End Sub
    186.  
    187. Private Sub Form_Load()
    188. 'Método casero para "Afinar" el numero total del bucle
    189. 'Intento hacerlo independiente del procesador
    190. Dim k&
    191. Dim Tpo! 'Para contar el tiempo
    192. Dim i&, j& 'Para el for
    193.  
    194. k = 1
    195. Do
    196.  Tpo = Timer
    197.  For i = 0 To 10000 * k
    198.  For j = 0 To 1000
    199.  If pVariable Then pVariable = pVariable
    200.  Next
    201.  Next
    202.  Tpo = Timer - Tpo
    203.  
    204.  TpoTotal = 10000 * k / Tpo
    205.  k = k * 10
    206. Loop While TpoTotal < 0
    207. txtRep = TpoTotal
    208. End Sub
    209.  
    210. Private Sub txtRep_Change()
    211. TpoTotal = txtRep
    212. End Sub


    Luego de ejecutar un par de veces cáda botón notamos que las velocidades son muy similares entre si, pero las variables globales (globales de formulario) son un poco más lentas.

    Sin embargo, si compilamos y ejecutamos nuevamente el código los resultados son un bastante diferentes.
    1º Las variables locales son bastante más veloces (aprox un 25%)
    2º Las variables globales (de módulo o de formulario) tardan senciblemente lo mismo.

    ERGO: Para optimizar en velocidad un código, NO USAR VARIABLES GLOBALES. Hacer una copia local de ellas en las funciones.
    Última edición por seba123neo; 19/08/2009 a las 04:03
    Iván (Vanchi) La Malfa
    OpenOffice.org
    .Free as in Freedom.

  2. #2
    crivadron está desconectado Junior Member Iniciad@
    Fecha de ingreso
    18 sep, 09
    Ubicación
    uio
    Mensajes
    22

    Talking

    Saludos, me parece interesane.. pero me puedes ayudar con el .frm

    no se me carga bien, tengo vb6

    gracias

  3. #3
    Vanchi está desconectado Moderador Veteran@
    Fecha de ingreso
    09 dic, 05
    Mensajes
    740

    Predeterminado

    Cita Iniciado por crivadron Ver mensaje
    Saludos, me parece interesane.. pero me puedes ayudar con el .frm

    no se me carga bien, tengo vb6
    gracias
    mmm... Si, yo hice lo mismo y tampoco me funcionó correctamente... Se ve que el higlight modificó algunas cositas.

    Te dejo acá lo que hay que copiar en el .frm para que el "highlight" no moleste.

    NOTA: Si querés mandame un mp con tu correo y te mando el form.
    NOTA2: No te olvidés de agregar un módulo que solo tenga por código esto:
    Public pVariable%

    Saludos.

    VERSION 5.00
    Begin VB.Form Form1
    BorderStyle = 1 'Fixed Single
    Caption = "Form1"
    ClientHeight = 3465
    ClientLeft = 45
    ClientTop = 330
    ClientWidth = 5520
    LinkTopic = "Form1"
    MaxButton = 0 'False
    MinButton = 0 'False
    ScaleHeight = 3465
    ScaleWidth = 5520
    StartUpPosition = 3 'Windows Default
    Begin VB.CommandButton cmdClear
    Caption = "Borrar listas"
    Height = 255
    Left = 3720
    TabIndex = 11
    Top = 120
    Width = 1695
    End
    Begin VB.TextBox txtRep
    Height = 285
    Left = 2160
    TabIndex = 10
    Text = "Text1"
    Top = 120
    Width = 1455
    End
    Begin VB.ListBox lstTiempos
    Height = 2010
    Index = 2
    Left = 3720
    TabIndex = 7
    Top = 960
    Width = 1695
    End
    Begin VB.CommandButton cmdOpciones
    Caption = "Locales"
    Height = 375
    Index = 2
    Left = 3720
    TabIndex = 6
    Top = 480
    Width = 1695
    End
    Begin VB.ListBox lstTiempos
    Height = 2010
    Index = 1
    Left = 1920
    TabIndex = 4
    Top = 960
    Width = 1695
    End
    Begin VB.CommandButton cmdOpciones
    Caption = "Globales"
    Height = 375
    Index = 1
    Left = 1920
    TabIndex = 3
    Top = 480
    Width = 1695
    End
    Begin VB.ListBox lstTiempos
    Height = 2010
    Index = 0
    Left = 120
    TabIndex = 1
    Top = 960
    Width = 1695
    End
    Begin VB.CommandButton cmdOpciones
    Caption = "Publicas"
    Height = 375
    Index = 0
    Left = 120
    TabIndex = 0
    Top = 480
    Width = 1695
    End
    Begin VB.Label Label2
    BorderStyle = 1 'Fixed Single
    Caption = "Número de repeticiones:"
    Height = 285
    Left = 120
    TabIndex = 9
    Top = 120
    Width = 2055
    End
    Begin VB.Label lblPromedio
    BorderStyle = 1 'Fixed Single
    Caption = "Promedio:"
    Height = 255
    Index = 2
    Left = 3720
    TabIndex = 8
    Top = 3120
    Width = 1695
    End
    Begin VB.Label lblPromedio
    BorderStyle = 1 'Fixed Single
    Caption = "Promedio:"
    Height = 255
    Index = 1
    Left = 1920
    TabIndex = 5
    Top = 3120
    Width = 1695
    End
    Begin VB.Label lblPromedio
    BorderStyle = 1 'Fixed Single
    Caption = "Promedio:"
    Height = 255
    Index = 0
    Left = 120
    TabIndex = 2
    Top = 3120
    Width = 1695
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    Dim TpoTotal&
    Dim gVariable%

    Private Sub cmdClear_Click()
    Dim i%
    For i = 0 To 2
    lstTiempos(i).Clear
    Next
    End Sub

    Private Sub cmdOpciones_Click(Index As Integer)
    Dim Tpo! 'Para contar el tiempo
    Dim i&, j& 'Para el for
    Dim lVariable%

    MousePointer = 11
    Tpo = Timer
    Select Case Index
    Case 0 'Publica
    For i = 0 To TpoTotal
    For j = 0 To 1000
    If pVariable Then pVariable = pVariable
    Next
    Next
    Case 1 'Global
    For i = 0 To TpoTotal
    For j = 0 To 1000
    If gVariable Then gVariable = gVariable
    Next
    Next
    Case 2 'Local
    For i = 0 To TpoTotal
    For j = 0 To 1000
    If lVariable Then lVariable = lVariable
    Next
    Next
    End Select
    Tpo = Timer - Tpo
    MousePointer = 0

    If Tpo < 0.1 Then
    MsgBox "El resultado no es confiable y está afectado de errores." & vbNewLine & "Se aumentó el número repeticiones del bucle", vbExclamation
    If Tpo < 0 Then
    txtRep = TpoTotal * 100
    Else
    txtRep = CLng(TpoTotal * 0.5 / Tpo)
    End If
    cmdClear_Click
    Exit Sub
    End If

    lstTiempos(Index).AddItem Tpo
    Tpo = 0
    For i = 0 To lstTiempos(Index).ListCount - 1
    Tpo = Tpo + lstTiempos(Index).List(i)
    Next
    lblPromedio(Index) = "Promedio: " & Tpo / lstTiempos(Index).ListCount
    End Sub

    Private Sub Form_Load()
    'Método casero para "Afinar" el numero total del bucle
    'Intento hacerlo independiente del procesador
    Dim k&
    Dim Tpo! 'Para contar el tiempo
    Dim i&, j& 'Para el for

    k = 1
    Do
    Tpo = Timer
    For i = 0 To 10000 * k
    For j = 0 To 1000
    If pVariable Then pVariable = pVariable
    Next
    Next
    Tpo = Timer - Tpo

    TpoTotal = 10000 * k / Tpo
    k = k * 10
    Loop While TpoTotal < 0
    txtRep = TpoTotal
    End Sub

    Private Sub txtRep_Change()
    TpoTotal = txtRep
    End Sub
    Iván (Vanchi) La Malfa
    OpenOffice.org
    .Free as in Freedom.

  4. #4
    belleza maquina está desconectado Junior Member Iniciad@
    Fecha de ingreso
    08 oct, 11
    Mensajes
    1

  5. #5
    moni_29 está desconectado Member Iniciad@
    Fecha de ingreso
    05 oct, 11
    Mensajes
    53

    Predeterminado

    muchas gracias por el dato. la verdad que nunca supe bien cuál era la diferencia y tampoco cuál era mejor y más veloz. ahora me ha quedado mucho más claro







    Vitnik

Temas similares

  1. Variables globales
    Por cestradah en el foro Visual Basic .NET
    Respuestas: 3
    Último mensaje: 13/05/2009, 13:48
  2. Variables publicas en varios proyectos de una solu
    Por JAE1 en el foro Visual Basic .NET
    Respuestas: 0
    Último mensaje: 16/05/2008, 14:30
  3. pasar variables globales
    Por sherif en el foro Visual Basic .NET
    Respuestas: 6
    Último mensaje: 24/06/2007, 18:42
  4. Problema con variables publicas
    Por Hardrocker en el foro Visual Basic 6.0
    Respuestas: 4
    Último mensaje: 04/10/2006, 03:41
  5. ayuda con variables globales
    Por Guests en el foro Visual Basic .NET
    Respuestas: 0
    Último mensaje: 13/06/2006, 19:54

Permisos de publicación

  • No puedes crear nuevos temas
  • No puedes responder temas
  • No puedes subir archivos adjuntos
  • No puedes editar tus mensajes
  •  
Visual Studio .VisualBasic.net .ADO.NET .ASP.NET .Framework .Crystal report
[Visual Basic .NET · Información legal · Condiciones de uso · Publicidad · Contacto · RSS novedades Foro · Inicio]
Un sitio web de Internelia (Ontecnia) © Copyright 2013 canalvisualbasic.net. Todos los derechos reservados