?
Solved

Make a code go faster...

Posted on 2005-03-18
27
Medium Priority
?
232 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:CodedK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 4
  • +4
27 Comments
 
LVL 2

Expert Comment

by:heretoread
ID: 13578130
you could remove Application.ProcessMessages;
0
 
LVL 16

Author Comment

by:CodedK
ID: 13578377
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13578832
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13578858
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13578888
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
 
LVL 17

Expert Comment

by:mokule
ID: 13578948
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13579014
Well, Edit1.Update will update only Edit1 control, other controls (and a whole application) will just hang up until procedure will be finished.
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13579113
>>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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13579218

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
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13579441
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
 
LVL 16

Author Comment

by:CodedK
ID: 13579509
@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
 
LVL 16

Author Comment

by:CodedK
ID: 13579521
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
 
LVL 16

Author Comment

by:CodedK
ID: 13579525
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13579576
CodedK, if you need better speed, try to incrase counter - it could help.
0
 
LVL 16

Author Comment

by:CodedK
ID: 13579620
ZhaawZ increasing the counter will make small difference..
e.x from 1000 to 2000 results.. :/
0
 
LVL 17

Accepted Solution

by:
mokule earned 1200 total points
ID: 13579799
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
 
LVL 46

Expert Comment

by:aikimark
ID: 13581212
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
 
LVL 16

Author Comment

by:CodedK
ID: 13581672
aikimark !!!!!!!! :?
0
 
LVL 16

Author Comment

by:CodedK
ID: 13581673
And cracks what?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13581742
cracking is analogous to hacking.  It looks like you are trying to generate passwords for some application in an attempt to circumvent security.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13582194
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
 
LVL 16

Author Comment

by:CodedK
ID: 13582230
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
 
LVL 16

Author Comment

by:CodedK
ID: 13582234
I also overreacted :(
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13582333
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question