[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TList  vs.   Vector  //   Delphi&Pascal  vs. C++   //   MS  vs. Borland .....

Posted on 2007-10-19
17
Medium Priority
?
1,751 Views
Last Modified: 2008-01-09
----  a general question ----

How to compare Pascal TList Class vs. C++  vector and Maps , in terms of performance  (insert, update, ...)and coding properties (readability of code, efficiency of the programmer)

Any good link or article is needed.

Is the lack of a STL in Delphi a major drawback, will this help C++ to stay a more imprortant programming language compared to Pascal?

Best

Bdlm
0
Comment
Question by:BdLm
  • 8
  • 4
  • 2
  • +3
17 Comments
 
LVL 29

Assisted Solution

by:pepr
pepr earned 240 total points
ID: 20108571
I do not know details of TList. There is a list in C++ that will probably be more similar abstraction to the TList.

It is not fair to compare Pascal and C++. The C++ implements the STL using templates. Then the versatility of such implementation is higher. I am not sure if this can be easily compared generally, without having some testing example.

For Borland vs. MS: The C++ from Microsoft is considered very good. And it is likely that it will be even better in future as Microsoft closely cooperates on the future C++ standard (Herb Sutter being their most visible expert in the area).
0
 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 240 total points
ID: 20109176
 You need to compare the tools versus a particular use... different tool sets excel for different tasks.  Delphi is my tool of choice for Windows application development.  The language is much more readable than C++ in my opinion (assuming the same programmer on both languages).

  One of the very things that can make Delphi code faster (the way it handles it's call stack) is the same thing that limits some of the functionality the C++ developers like to bring up (how each language has to go about handling variable parameter lists).  The speed can be reached in C++ code as well, by using the same call stack approach (PASCAL), but then you limit your call stack the same way Delphi's is limited.

  Many C++ developers tout operator overloading as a major language enhancement that Delphi does not have.  I have only found limited areas that this is useful in my own code (one project in 20 years).  Delphi for .NET does allow operator overloading, not the Win32 variety as I understand it.

  The Delphi compiler is FAR faster than the C++ compiler.  In one instance I sat down with a person familiar with C++ compilers and showed him my delphi application running in the IDE.  He asked me, "Ok, now... what does it take to compile it into an application?".  I stared at him confused for a moment because it was alrady a running application (he had just witnessed the entire compilation progress and missed it).  This speed difference can make a huge impact on the time it takes to go through the development cycle.

  Having said all of that, C++ has uses that Delphi does not handle.  If you need to write an operating system, BIOS, or even code that will reside on a hardware board C/C++ is the choice.  Delphi is not designed for that kind of use.  If you want to write code that handles multiple OSes then C++ is likely your best bet as well (as a compiled language with native executable output... I would probably prefer Java or several others if native EXE output is not the goal).

...as a side note...
  For most development I would stay away form .NET... it is prone to cause "Pregnant pauses" during execution.  I know that many developers will claim that the garbage collection in .NET is so good that you will never see a pause from it... I have (1 to 5 seconds).  At least in the Delphi / C++ languages where you manage your own memory you do not run into unpredictable garbage collection delays.

  No one tool is not the best for all purposes.  Evaluate what you will be doing and make your decision based on that.  If most of what you will be doing is Windows development then, personally, I don't think anything comes remotely close to Delphi for being able to produce reliable, fast, code - quickly.  If you plan to do Windows development and other types, I would use multiple tools.  Choose the one that is right for your task.
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 20112589
Realistically, Borland is a small abberation, and even if you don't love Microsoft (I don't) the world uses C++, Java, and C#. So in terms of the market, Delphi is a lost cause. I'm not commenting on the merits, because I don't know Delphi.

In terms of compile time, Java with Eclipse is very fast because it's incrmenetal, though it's not as fast as Delphi, probably. And that does make a big difference in programmer productivity. C++ has the most expressive power, which is occasionally really useful. When writing mathematical code, the ability to use overloaded operators and have vector equations look as they should is unbeatable.

Templates are a compile time mechanism that is a double win -- the resulting code can be super fast and efficient, while types are checked at compile time. If I want a list of Elephants, I don't want zebras creeping in there, only to crash at runtime, I want to know at compile time that I did something illegal.

0
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!

 
LVL 8

Author Comment

by:BdLm
ID: 20116073
     The delphi / pacal code ...
------------------------------------------------------
      //  loop through all elements
      for i := 0 to MyNetList.Count -1 do
         begin
           myElement := MyNetList.Items[i];
           for j:=0 to MyElement.Nodes.Count-1 do
              begin
              If BiasNodes.IndexOf(MyElement.Nodes[j])=-1 then BiasNodes.Add(MyElement.Nodes[j]);
              end;
         end;


C++  STL   Code
---------------------

    for(std::vector<unsigned long>::const_iterator i = m_VectorElementID.begin();
            i!= m_VectorElementID.end(); ++i)
      {
            if(*i==id)
                  return ;            
      }
      m_VectorElementID.push_back(id);
      return;


here a short code comparisons, not same functionallity, but same complexity.
I can't post both applications code here.

We used a first application, written in pascal to read a txt file with 50,000 data records, store them in a Tlist, do some tiny modifications and dump the new List in  an XML File.  We used MSXML 6 API for  writeing  the XML file.  Run Time for 50.000 recoreds a  ~  minutes.

The second application, written in C++ reads this XML file, store the data in a MAP (dynamic array, our replacement for the TList Class from Pacal) and finally we have  to do other operations. Just reading the XML  and creating the MAP takes more then 2 hours. Again we used MS XML. With this ~ 100 times slower data processing we can not reach our performance targets

We have to access single records from our dynamic array, we have to modify single elements in this array, therefore we think, The pacal TList is much better than a vector or a Map from STL C++.
I want to get proofs or inputs how to compare this.  
0
 
LVL 29

Expert Comment

by:pepr
ID: 20121109
I believe that the problem is somewhere between the keyboard and your chair. I cannot see any similarity between the two code fragments. The control structures are completely different... also the operated data. It is impossible to comment on your XML example.

Some more comments: The std::map is not a dynamic array, it is a look-up table (the term map is generally used for that kind of data structure). The std::vector is a dynamic array, but it is not a list. List is not an array and if indexing of the list is syntactically allowed, then it will always be slower than the std::vector (in principle).

You should show how your XML records look like and what kind of processing you do. If processing the 50.000 records takes 2 hours, then you are processing about 7 records per second which is unrealistic for any usual program (even written in the scripting language).
0
 
LVL 8

Author Comment

by:BdLm
ID: 20121546
in both programs we init a dynamic array of a  defined Datastructure.   Data stuct look like
AElement =  .....  many type    subDynArray  :  Dynamic array  of  ASubdatatype;
What we have to execute in our program is llok for a Index of an Element, find the Index of a Subdatatype and to some small calc.
Our believe is, that the Indexof, Find an Element, Loop thorugh all elements is significant slower for vector and map  as for TList.  

For coorect analysis we should write the identical code in both languages and do our tests,
but I want to avoid this effort wuth with discussion here, looking for a explanation.
         
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 240 total points
ID: 20121990
From what I have read about TList (http://www.delphibasics.co.uk/RTL.asp?Name=TList), it seems that it is rather a dynamic array (than list) with some extra functionality. Arrays (including std::vector) are not the best choice for a structure that should be searched for some content.

Notice that you can use also indexing when accessing the element inside the std::vector -- and it is extremely fast operation. Try this:

=============================================
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<long> v;
    size_t vMax = 50000;

    cout << "reserve...";
    v.reserve(vMax);        # only to optimize allocation

    cout << "\nassign...";
    for (size_t i = 0; i < vMax; ++i)
    {
        if (i % 1000 == 0)
            cout << " " << i;
        v.push_back(i);
    }

   
    cout << "\nloop...";
    for (size_t i = 0; i < v.size(); ++i)
    {
        if (i % 1000 == 0)
            cout << i << ": " << v[i] << "\n";
    }

    cout << "\n(the end)\n";

    return 0;
}
=============================================
0
 
LVL 29

Expert Comment

by:pepr
ID: 20122022
If you really want to search for a content based on some key (say identification), you should use one of the avaliable map<> structures. Try this:

=============================================
#include <map>
#include <iostream>

using namespace std;

int main()
{
    map<long, size_t> m;
    long MAX = 50000;
   
    cout << "\nassign...";
    for (long i = 0; i < MAX; ++i)
    {
        long id = i * 2;         // just to simulate the ID
        size_t content = i * 3;  // ... and the content
        m[id] = content;         // insert the content for the id
    }


    cout << "\n";    
    cout << "id = 4; value = " << m[4] << "\n";
    cout << "id = 30000; value = " << m[30000] << "\n";

    cout << "\n(the end)\n";

    return 0;
}
=============================================

0
 
LVL 29

Expert Comment

by:pepr
ID: 20122031
Use "// only to optimize allocation" in the previous example instead of "# ..." (my mistake).
0
 
LVL 8

Author Comment

by:BdLm
ID: 20122354
many thanks in advance for you post,  I will check this code and than accept the question
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 150 total points
ID: 20123495
>>>> Arrays (including std::vector) are not the best choice
>>>> for a structure that should be searched for some content.

That depends on the number of items and the order the entries were inserted. In case of less than 100 entries the differences in speed are some nanoseconds and you can go for the container you know best. For higher counts and in case you have a sorted array (TList or std::vector), you can lookup for a given value using a binary search algorithm (check whether the 'middle' item of the array is less or greater than the item searched for. Take either low or high part of array to search again, and so on ...) which is as fast as lookup in std::map but requires less storage and most likely quicker for insert than putting all pairs of key and value to a std::map. Of course using std::map (or an equivalent dictionary class in Borland) is much more comfortable than having to implement a binary search yourself. But as it is a one-time job of less than an hour work, I wouldn't made that a main criteria.

Regards, Alex
0
 
LVL 29

Expert Comment

by:pepr
ID: 20129148
BdLm: "We used a first application, written in pascal to read a txt file with 50,000 data records, store them in a Tlist, do some tiny modifications and dump the new List in  an XML File."

The question is whether you should not focuse more on searching for the "right" solution instead of focusing on improving the ad-hoc solution copied from the old application. In my opinion, you should learn basics of XML processing. It seems that some implementation of a SAX based parser may be the right approach for you (http://en.wikipedia.org/wiki/SAX). As you are using Windows, you can use MSXML components by Microsoft (http://en.wikipedia.org/wiki/MSXML) or XmlLite (http://msdn2.microsoft.com/en-us/library/ms752872.aspx).

I personally would probably start with samples that use XmlLite (http://msdn2.microsoft.com/en-us/library/ms752864.aspx).
0
 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 240 total points
ID: 20132556
pepr:
It's all well and good to use out of the box solutions to problems, and most of the time they work out well.  The only thing I hate to see in that regard is when developers become incapable of designing new solutions on their own due to the thought that there ar already "acceptable design patterns" that exist for this case.  While your program MUST be accurate, user friendly, etc you cannot deny the need for speed.  If, in any given situation, a design pattern you have inherited is not producing the results you want/need then by all means get creative!  I don't think any of this this addresses the original question though.

  I have not personally missed the C++ STL at all (while using Delphi).  Most templates I would need already have an implementation, or are easily derived (in minutes).  Now, having said that, you could easily do a TList in C++.  This would result in the same sort of speed you have in Delphi (with the exception of call stack performace, etc).  It would also give you a tool that is more comfortable to you if you are familiar with Delphi.  As I stated before, what it really comes down to is what you need for THIS project (any of the many you work on).  Make your decision on a case by case basis and you will optiize your development efforts vs end product quality every time.
0
 
LVL 29

Expert Comment

by:pepr
ID: 20136728
developmentguru: Well, it is always good to think about a solution. I agree that "rely on obscure 3rd party library" only because someone said it can be used is not good. However, "reinventing the wheel" in the case it is just around the corner is also not good. It is good to learn and to work hard all the time. However, in my opinion, it is better to use your creativity for the things that have not been solved, yet.

There is no "the best programming language". Both C++ and Delphi are not exceptions. But I have to partly agree with dbkruger about positions of the two languages. I believe that C++ is still more powerful and versatile. The dark side of C++ is that you have to learn more to be efficient in it. But after that, you are not limited. You can use your creativity the way it cannot sometimes be done in other languages.

To be short and back to the original question... I strongly believe that simply "using C++ instead of Delphi" is the reason for slowdown computation.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 240 total points
ID: 20136735
correction: "I strongly believe that simply "using C++ instead of Delphi" is *NOT* the reason for slowdown the computation."

Of course... ;)
0
 
LVL 1

Assisted Solution

by:mipko
mipko earned 120 total points
ID: 20197354
check DCL for delphi.

http://sourceforge.net/projects/dclx

I think you will find very nice stuff in it.

Mirko
0
 
LVL 8

Author Comment

by:BdLm
ID: 20341301


We evaluated the code in more detail, as we use many insert elements in a list, search for a Element in List, we can't compare directly our delphi and our C++ application.  .-((

the Delphiapplication  does the operation in one direction , with the c++ app. we calculated the opposite direction.

MSXML and the msxl.dll seem to work fine for both compilers. The difference in performance is created by a difference in List and vector operations. we have to eval. out code in more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Make the most of your online learning experience.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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