Results 1 to 5 of 5

Thread: Can this be optimize

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Can this be optimize

    Hello, i did a script that shuffle number from 0 to 7,
    just need to know if it can be optimize

    VB Code:
    1. Randomize
    2.  
    3. Dim rndArr(), tmp, isOk, x
    4.  
    5. x = 0
    6. ReDim rndArr(x)
    7.  
    8. Do Until x = 8
    9.     tmp = CInt(Rnd() * 7)
    10.     isOk = True
    11.     For y = 0 To x - 1
    12.         If rndArr(y) = tmp Then
    13.             isOk = False
    14.             Exit For
    15.         End If
    16.     Next
    17.     If isOk = True Then
    18.         rndArr(x) = tmp
    19.         x = x + 1
    20.         ReDim Preserve rndArr(x)
    21.     End If
    22. Loop

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Yes!

    Type cast all of your variables. isOk is boolean; rndArr(), tmp, x and y are integers. Numeric variants use 16 bytes of memory, integers and booleans use 2. Then there is the overhead required to manipulate them.

    Since you know rndArr will have 8 elements, Dim it as such and stop ReDimming. ReDim also has a lot of overhead.

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you cant type cast in vbscript ccoder. eveything is a variant in vbscript.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    2 thing, first of all it's asp, so there's no

    Dim x As Integer

    your right about the 2nd thing, it's because at first
    i was looping thru the rndArr, but i change my code and
    now it would be ok!!so here's the changes

    VB Code:
    1. Randomize
    2.  
    3. Dim rndArr(7), tmp, isOk, x
    4.  
    5. x = 0
    6. Do Until x = 8
    7.     tmp = CInt(Rnd() * 7)
    8.     isOk = True
    9.     For y = 0 To x - 1
    10.         If rndArr(y) = tmp Then
    11.             isOk = False
    12.             Exit For
    13.         End If
    14.     Next
    15.     If isOk Then
    16.         rndArr(x) = tmp
    17.         x = x + 1
    18.     End If
    19. Loop

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by Cander
    you cant type cast in vbscript ccoder. eveything is a variant in vbscript.
    D'oh! I see that now Guess I read the post too fast.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width