Code optimization & dll query

Discussion in 'Questions (Windows Mobile)' started by gangsta, Mar 26, 2008.

  1. gangsta

    gangsta Member

    Hi all, first off - ive only had basic4ppc for about 4 days, but im really loving it. That me onto my forth program now - so i think i'll be buying the full program soon. I work in a petrol station, and find that when its quiet i can sit and do my programming without any problems, really does make the days go by quicker :) .

    Anyway back on topic.

    I have wrote a bit of code, but it takes too long to run (well about 10 seconds) I know that what i want can be done almost instantly but i dont have my thinking cap on.

    Can anyone optimize this code for me, either by shifting bits, multiplication or whatever.

    Code:
    dim tmpcode(3)
    dim codecount,loopa,loopb,inputnum

    tmpcode(
    0)=1
    tmpcode(
    1)=1
    tmpcode(
    2)=1
    tmpcode(
    3)=1

    inputnum = *****a number between 
    0 and 1023*****

    for loopa = 0 to codecount - 1
     tmpcode(
    3) = tmpcode(3) + 1
     
    for loopb = 3 to 1 step -1
      
    if tmpcode(loopb) = 7 then
       tmpcode(loopb) = 
    1
       tmpcode(loopb - 
    1) = tmpcode (loopb - 1) + 1
      
    end if
     
    next loopb
    next loopa

    txtOutput.text = tmpcode(
    0) & tmpcode(1) & tmpcode(2) & tmpcode(3)
    so that is the first problem.

    the second is that i have a '3 step program', just now i am using panels for the different steps and hiding and showing them as needed.

    the program is like this.

    Step 1... choose an item from a combo box

    Step 2... show the panel corresponding to the option chosen in Step 1

    this panel could include combo boxes, text boxes, check boxes or whatever. clicking 'next' on this panel does a calculation on the data given in the panel, and passes it on to panel 3

    Step 3... Show the result, this will be a number of indeterminate length, usually 4-8 digits


    So far i have created 6 option for step 1, therefore i have 6 panel 2's, and its starting to get confusing. Im also worried about using too many controls on a single form (up to about 100 or so, so far) and i want to bump it up to around 50 options (therefore 50 panels * about 12 controls each)

    I imagine that will also start to slow down the program.

    It also means that i need to supply a full new program each time i make a change.

    What i want to do.

    I want the program to start,... then look through the app directory for DLL's, each one it finds - it interrogates, to get its name, and adds it the list in step 1.

    If you choose an option, then for step 2, the THE DLL SHOWS A PANEL/FORM/WHATEVER that looks like part of the host application, when the DLL panel has been filled, it hides its panel, does its maths (different for each DLL) and returns the number for my program to display on the Step 3 page.

    Is this possible to use DLLs in this way, can they be dynamically loaded at run time, can they display information in my application.

    This would mean that as i develop new algorithms, i just need to send out new DLLs, or if i need to alter an algorithm, again just a replacement DLL.

    Id like to add the ability to download additional DLL's over the air, then add them dynamically to the list of options.

    If im going about this all wrong, please can you point me in the right direction.

    Thanks

    Graham
     
  2. gangsta

    gangsta Member

    no takers on the code optimization??

    what about the dll query, is it possible for a dll to display with a panel within my program, or can it display a box over my program??

    thanks

    graham
     
  3. LineCutter

    LineCutter Active Member Licensed User

    7 mod 7 = 0

    The code optimisation:
    Looks like you're using iterations to deal with the need to do maths!
    I think that you're missing out on 'mod'

    What does inputnum do & where does codecount come from?

    Come to think of it you could probably make life simpler by avoiding the array if it's only use is to create the string at the end. Use mod & your 7 times tables (but don't forget to add 1 to the mod result, you'll get 0 when you want 1).

    (The lack of a simple code solution is deliberate here, you'll hit similar problems later on - this is a good one to cut your teeth on. Little snippets of code to test the workings, as you go, will help you get the hang of it.)
     
  4. gangsta

    gangsta Member

    hi thanks, i get what you mean. I will have a look at the mod command, i knew it could be done easier, but i havn't done any programming since microsoft decided to move over to .NET.


    I have been programming on the windows for many years, but it was always business orientated, and no complex maths involved.

    All my experience has been on VB5 and 6.

    I now have have my program doing 18 algorithms, but am still using panels.


    ** i paraphrased the code a bit so i was only posting relevant code **
    InputNum comes from either a textbox for some of the algorithms or a set of checkboxes (10 bit binary).

    The reason for the array is that i use many different algorithms which do things in different ways. so i figured use an array so that my code display page runs without any modification regardless of which algorithm im using at that time.

    I am having to work out my algorithms from a list of input numbers = codes, thats why my code isn't optimized, as i dont have the original algorithms used to generate the codes.

    Basically i work out how the code from the input number, then try and come up with an algorithm that will solve it. Once it works i try and optimize it to work faster.

    Basically on my pocket pc, and with the nature of the codes i should ALWAYS be able to calculate the code within 1 second.

    i'll work on the mod thing, and post back if i solve it. thanks.

    can a dll show a panel within my program??
     
    Last edited: Mar 28, 2008
  5. gangsta

    gangsta Member

    just had a look in the online help, and couldn't find the MOD function, is it implimented in basic4ppc???

    if not can i use this formula to simulate the mod fuction (from wikipedia):

    mod(a, n) = a - n * floor(a / n)

    i have substituted INT in place of floor in the hope that it will still work.
    which i would impliment in code as something like:

    Code:
    Sub Mod (a, n)
      
    Return (a - n) * int(a / n)
    End Sub
    ive never done any crypto stuff before but most of the algorithms ive looked at so far have been not too dificult (although as seen above, a few need some optimization)
     
  6. agraham

    agraham Expert Licensed User

    Yes. See Help->Main Help->Basics->Operators

    Do you mean a real dll as in a .NET assembly or native dll? What are you going to compile them with? In principle a dll can build a panel with some controls and put it on a Form specified by the main program.
     
  7. klaus

    klaus Expert Licensed User

    Hi gangsta,
    Mod is not a function but an operator, and is implemented in B4PPC.

    Ex:
    a = 7 mod 3 result 1
    The function means 7 devided 3 equals 2 and the reminder is 1, a is the reminder
    a = 7 Mod 3 result 1
    a = 8 Mod 3 result 2
    a = 9 Mod 3 result 0
    a = 10 Mod 3 result 1
    and so on.

    I have added a small program you can play with.

    For your example in the first post, could you explain exactly what you want to do or add some more code with an example to make it easier for others to give you any advice.

    Best regards
     
  8. gangsta

    gangsta Member

    Hi the program is for decoding.

    Eg you supply it with the serial number for the device, and it applys the algorithm to it, to give the unlock code.

    I have visual basic enterprise edition , can i use that for building dll's to use within basic4ppc or do i have to buy .NET (or will visual studio express work for this)

    My posted code at the top of the page works perfectly for this algorithm, its just slow :(

    i have been playing with mod, had a look at the demo, so hopefully i'll work it out shortly :)

    thanks

    Graham
     
  9. gangsta

    gangsta Member

    woohoo :)

    thanks people, I have wrote a small base convertor based on what I found on the net, it will convert any integer to any other base, but be warned - no boundry checks - I did a stpid mistake when testing it an converted to base 1, my ppc was NOT amused - lol

    thanks for pointing me in the right direction, I actually enjoy learning new things, and don't mind looking stuff up if im pointed in the right direction.

    Graham

    Code:
    Sub convertBase(iNum, bNum)
    Dim q, r, a
    q = iNum
    r = iNum
    Do While q <> 0
     r = q 
    Mod bNum
     q = Int(q / bNum)
     a = r & a
    Loop
    return a
    End Sub
     
    Last edited: Mar 29, 2008
  10. agraham

    agraham Expert Licensed User

    I assume that is VB 6.0 Enterprise in which case the answer is no. VS Express will not produce dlls for mobile devices but the free Sharp Develop http://www.icsharpcode.net/OpenSource/SD/ together with the free SDK from Microsoft will produce device targeted dlls (they also usually work on the dekstop). Severasl members of this forum use it, just post a request for advice (I use VS2005 Standard Edition).
     
Loading...