Make a code go faster...

Hi.

How can i make this code go faster?

procedure TForm1.Button1Click(Sender: TObject);
Begin
  Repeat
  Number:=Random(MaxNumber);
  Edit2.Text:=IntToStr(Number);
  E2:=StrToInt(Edit2.Text);
 until (E1<>E2)and(E2<>E3)and(E2<>E4)and(E2<>E5)and(E2<>0)
 end;
End;

---------------------------------------------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
Begin

      Repeat
        BEGIN
        Button1Click(Sender);
        Application.ProcessMessages;
        END
       Until Something;
End;


The think is that i get about 1000 results per sec....
Other programs achieve 10.000.000 per sec..


Thanks in advance.
LVL 16
CodedKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heretoreadCommented:
you could remove Application.ProcessMessages;
0
CodedKAuthor Commented:
Nop...
I want to see results...

Anyway the thing is that there is a big gap between 1000 per sec and 10.000.000 /sec.
Even if i remove ProcessMessages i will gain about 1000 values... :/

The question is not particular with this code... This is just an example....
I want to know how can i make a value change in an editbox about 10000000 times per sec...
Like the applications that search for a password...

0
ZhaawZSoftware DeveloperCommented:
CodedK, hmm.. You said that you'd like to know how to change txt in editbox about 10000000 (!) times per sec.. Do you really need it? Can you read all those values that fast?! :)

Changing value of editbox takes a loot of time (freeing it from previous value, allocating memory for the new one, assigning new value, in your example - also converting number to string (also allocating memory), displaying etc). If it changes about 1000 times per sec, it's crazyness to do this!

Try to add new variable - a counter, that would help you to display results after, lets say, 10'000 cycles. It could be something like this:

var
  cnt : integer;
begin
repeat
  inc(cnt);
  if cnt = 10000 then begin
    cnt := 0;
    edit1.text := format('%d', [value]);
    application.processmessages;
  end;
  //other operations - whatever you need
until false;
end;


Test it, change 10'000 if you're not satisfied with speed of seeing values. This should be a much easier to do for CPU/RAM, the speed is also much higher and you'll be able to see current results.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ZhaawZSoftware DeveloperCommented:
Also you could test different ways of converting integer to string. There are functions IntToStr(), Format(), a procedure Val(). You could also try to write your own one to improve the speed.

One more thing about your example:
  Edit2.Text:=IntToStr(Number);
  E2:=StrToInt(Edit2.Text);

This is a bit "crazy" :) It should be:
  Edit2.Text:=IntToStr(Number);
  E2:=Number;

Try to avoid unnecessary convertations when it is possible - these operations take a loot of resources (mainly it's CPU, afaik)
0
ZhaawZSoftware DeveloperCommented:
One more thing - try to avoid involving unnecessary variables.

repeat
  Number:=Random(MaxNumber);
  Edit2.Text:=IntToStr(Number);
  E2:=StrToInt(Edit2.Text);
until (E1<>E2)and(E2<>E3)and(E2<>E4)and(E2<>E5)and(E2<>0)

It should be:

repeat
  e2 := random(maxnumber);
  edit2.text := inttostr(e2);
until (E1<>E2)and(E2<>E3)and(E2<>E4)and(E2<>E5)and(E2<>0)
0
mokuleCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  tb: TDateTime;
begin
  tb := Now;
  for i := 0 to 10000 do
    begin
    Edit1.Text := IntToStr(i);
//    Application.ProcessMessages;
    Edit1.Update;                // this is faster than Application.ProcessMessages;
    end;
  Edit2.Text := IntToStr(Round(MSECSPERDAY*(Now - tb)));
end;

mokule
0
ZhaawZSoftware DeveloperCommented:
Well, Edit1.Update will update only Edit1 control, other controls (and a whole application) will just hang up until procedure will be finished.
0
BlackTigerXCommented:
>>How can i make this code go faster?

procedure TForm1.Button1Click(Sender: TObject);
Begin
  Repeat
  Number:=Random(MaxNumber);
  Edit2.Text:=IntToStr(Number);
  E2:=StrToInt(Edit2.Text);
 until (E1<>E2)and(E2<>E3)and(E2<>E4)and(E2<>E5)and(E2<>0)
 end;
End;

don't use a Edit box to store the string, using a EditBox as a *variable* is slowing you down WAY too much there, just use a string, if you want to update the screen then pass it to the EditBox
0
Russell LibbySoftware Engineer, Advisory Commented:

The belief that you can actually update the text in an Edit window 10 million times a second is an illusion; you just can't do it. Even at a low level of:

var i: Integer;
begin
  for i:=1 to 10000000 do
      SendMessage(Edit2.Handle, WM_SETTEXT, 0, Integer(PChar(IntToStr(i))));
end;

It will take just shy of 2 minutes (on a P4) for this to complete. That is not even with the UpdateWindow call, which is what happens when you call Edit2.Update. That is not to say that you can't give the **appearance** of updating 10 million times a second though...

As ZhaawZ said, update it only every N iterations to give the desired appearance.

var  st, et, i:     LongWord;
begin

  st:=GetTickCount;
  for i:=1 to 10000000 do
  begin
     if ((i mod 1000) = 0) then
     begin
        SendMessage(Edit2.Handle, WM_SETTEXT, 0, Integer(PChar(IntToStr(i))));
        UpdateWindow(Edit2.Handle);
     end;
  end;
  et:=GetTickCount-st;
  Caption:=Format('%d ms', [et]); // Takes 719 ms on my system

end;

This is what other applications do when displaying a large listing of items. Trying to display each and every one is just not feasible.

Russell





0
BlackTigerXCommented:
I think most (all?) of us agree the problem comes down to that, updating the EditBox on every iteration, that's what is slowing your code down
0
CodedKAuthor Commented:
@ZhaawZ
--->This is a bit "crazy" :)

Yes i know i just copy pasted a code ... didnt even see it. :/
The code was just an example.
I know that i cant get the results of 10000000/sec when i show every value in a textbox...
Its very logic to add a counter... The thing is to say it clear enough that what ever i do
Process messages or not... Showing the results on a textbox or not... I cant get more results
but just ~1000 in a sec....


@ Mokule and ZhaawZ
Edit1.Update is a nice thought... :)

@BlackTigerX

1.) Store it to a String
2.) Pass the String to an EditBox

Isn't that equal (in a way) with
1.) Convert number to string
2.) EditBox = String
???


@Russell

:/ I know guys i shouldnt post this code....
I have a really number sequence... and i want to find a possibillity... there is a code behind...
Mostly repeat... until... something true, bypasing the numbers that allready appeared...
~1000 per sec is the limit....
Think of it as
Application process and edit isnt there...
But there are some applications built with delphi ... like Advanced Zip password Recovery for example...
that search a zip file for passwords.. I know that in brute force that thing should try every possible combination...
but the speed on my machine is ~10000000 words/sec...
(They adverise their product about the speed too...)
And the funny thing is that u can set the interupt of an editbox update even in 0.5 secs and still not getting
any loss in speed....

Thanks for ur time... :)









0
CodedKAuthor Commented:
So i want to know a really simple routine... with

Repeat
...
Something
....
Counter...
Show something in editbox... when counter is 1000...
....
end until something;

0
CodedKAuthor Commented:
that give a result about 1000000 per sec... If that is not possible...
i'll split points guys...
maybe its low level inside that apps i m talking about dont know...
0
ZhaawZSoftware DeveloperCommented:
CodedK, if you need better speed, try to incrase counter - it could help.
0
CodedKAuthor Commented:
ZhaawZ increasing the counter will make small difference..
e.x from 1000 to 2000 results.. :/
0
mokuleCommented:
Place four Edits on a form and a Button.
Enter some low letter word into edit1.
You will have time elapsed in Edit2 and count in edit4

procedure TForm1.Button2Click(Sender: TObject);
var
  c2,c3,c4,c5,c6: Char;
  s: string;
  tb: TDateTime;
  i: integer;
label
  kon;
begin
  tb := Now;
  s := Edit1.Text;
  s := Copy(Trim(s)+'aaaaaa',1,5);
  i := 0;
  for c2 := 'a' to 'z' do
    begin
    for c3 := 'a' to 'z' do
      begin
      for c4 := 'a' to 'z' do
        begin
        for c5 := 'a' to 'z' do
          begin
          for c6 := 'a' to 'z' do
            begin
            Inc(i);
            if s=c2+c3+c4+c5+c6 then
              begin
              Edit3.Text := 'found';
              Edit4.Text := IntToStr(i);
              Edit1.Text := c2+c3+c4+c5+c6;
              goto kon;
              end;
            end;
          end;
        Edit1.Text := c2+c3+c4;
        Edit1.Update;
        end;
      end;
    end;
kon:
  Edit2.Text := IntToStr(Round(MSECSPERDAY*(Now - tb)));
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
This appears to be a cracking question, which is a violation of EE rules.  I have alerted Community Support to look at this discussion thread.
0
CodedKAuthor Commented:
aikimark !!!!!!!! :?
0
CodedKAuthor Commented:
And cracks what?
0
aikimarkCommented:
cracking is analogous to hacking.  It looks like you are trying to generate passwords for some application in an attempt to circumvent security.
0
aikimarkCommented:
CodedK,

You wrote:
''But there are some applications built with delphi ... like Advanced Zip password Recovery ..."

It raised a flag, since you intend to create software whose purpose might be construed as "cracking", which we are not allowed to do as it violates the EE terms of use to which we all agreed.  

Please convince us otherwise.  I might very well be wrong.
0
CodedKAuthor Commented:
Well aikimark i'm building a program....and i mentioned that, that searches for possibilities ....for "lotto - joker"
.... gample games...

Advanced Zip Password Recovery by Elcomsoft (www.elcomsoft.com) was the only application that i had in mind that
CAN give about 10.000.000 results /sec with updating the EditBox in the form...
created with Delphi and not some low level lang.

Further more AZPR is a commercial application and DOES not belong to the family of cracking programs.
I also mentioned the option inside AZPR --> Brute Force method
coz this is the method that creates random numbers..... and then without repeating the same numbers
(exactly like the application i'm building) it can give 10000000 results.

When i posted this question i gave an example code of a repeat routine and asked how is it possible
even with updating the form to get this number of results... with a simple repeat routine.

Thats it. :/

0
CodedKAuthor Commented:
I also overreacted :(
0
aikimarkCommented:
CodedK,

AZPR may be making a direct procedure call with a password, rather than passing the password through a textbox control.  The updating of a textbox control may occur every 10000 attempts as the posted comments have illustrated.

As ee_ai_construct correctly states, we are not accusing you of anything nefarious with your question.  Your AZPR reference just raised the possibility that this 'might' be a crack and I am obligated to keep the moderators informed on such possibilities.  Nothing personal against you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.