Re: How to make Delphi code quicker



Valerij Rozouvan wrote:
....
Am I doing something wrong, or there
is simply no way to speed up Delphi code without
abandoning Delphi?

First and foremost: look at the algorithm! There is a time/space tradeoff. Means: you can get a lower time complexity by allowing a higher memory complexity. Roughly translated: buffers are great! Store anything you might need later, in the next iteration, whatever. Store everything that might be useful as a partial result of a calculation. If you are working with arrays: use helper arrays, one dimension smaller, for caching. Then go to the stranger parts: try different loops: for/next, while, repeat, count up or down. Try different data types, maybe you don't need integers everywhere and get better results by cramping more data in the level 1 cache. Don't address the same array element over and over again, instead push it in a variable. Of course, try to do the work in two or more threads! There are so many HT and dualcore CPUs today, it really helps. Most of the time :-)

And _then_, at the end: become a wizard. Go directly to the memory. Use pointers, increment them instead of addressing an array element with an index. Use Move, or better, FastMove. Take a look at the assembler code: any way to do something to give the compiler another register?

The last step can give you about 25%. BUT: the code becomes really ugly. And all the stuff before that can give 80% speed increase (eg. 20 seconds instead of 100). Once I had a contract to optimize a function that works an _really_ large arrays. I brought it down from about 40 seconds to .1 or so. One year later, I took fresh look, got another contract for the same thing, and now it's calculated in .02 seconds. And that means pattern recognition in an array with more than one million elements. But now I am pretty sure that there is nothing you can do while using Delphi to make it fast. Funny: for this function, a cheap Sempron 3100+ is almost as fast as a P4 3.2 HT...

Ralf

.



Relevant Pages

  • Re: IP Level Encryption
    ... wasn't my point that you should avoid C and use Delphi ... than in C with its string type and dynamic arrays. ... Judging by a lot of code out there, fixed-length arrays are common ... strings (as in an array of chars, not the string type) are quite rare unless ...
    (sci.crypt)
  • Re: Safe pointer arithmetic and typecasts :D
    ... >> I always wondered how to do safe pointer aritmetic in delphi without ... > There is no really safe method of doing pointer arithmatic in any ... > turned on the program is going to catch any invalid indexes. ... Using arrays and indices instead would make it even more difficult. ...
    (alt.comp.lang.borland-delphi)
  • C++-DLL in Delphi nutzen
    ... Ich binde eigene mit Delphi erzeugte DLL's dynamisch wie folgt in mein ... Spl: TSplProc; ... Verwende ich statische Arrays, so funktionierts auch. ... Oder geht das Ganze gar nicht mit dynamischen Arrays? ...
    (de.comp.lang.delphi.misc)
  • Re: Compiler optimisation
    ... and C++ compiler that can beat Delphi 5-10x ... in floating point and integer math that uses arrays. ... The only are where Delphi is still "in class" ...
    (borland.public.delphi.non-technical)
  • Speed of large [] array
    ... With vc7 I am having trouble with large arrays slowing down when I access ... I thought getting an array element was ... // Times are for the statement executing 5,000,000 times in a loop ... // small constant index ...
    (microsoft.public.vc.language)