Python performance notes...

Brett g Porter BgPorter at NOacmSPAM.org
Sat May 27 23:13:12 CEST 2000


"Moshe Zadka" <moshez at math.huji.ac.il> wrote in message
news:Pine.GSO.4.10.10005271201030.4282-100000 at sundial...
> On Thu, 25 May 2000, Brett g Porter wrote:
>
> Well, of course, it is much longer in C++, but just for the heck of it,
> here's the real challenge: change it so you'd print
>
> word lineno lineno .....
>
> And the words have to be sorted.
// splitter.cpp : Defines the entry point for the console application.
//  (c) 2000 Bg Porter

#include "stdafx.h"

#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

int main(int argc, char* argv[])
{
   ifstream f(argv[1]);

   string line;
   string newWord;
   int lineNum = 0;
   map<string, vector<int> > unique;

   while (f)
   {
      getline(f, line);
      istringstream lineStream(line.c_str());
      ++lineNum;
      while (lineStream)
      {
         lineStream >> newWord;
         unique[newWord].push_back(lineNum);
      }
   }

   for (map<string, vector<int> >::iterator i = unique.begin();
   i != unique.end(); ++i)
   {
      cout << i->first;
      for (int j = 0; j < i->second.size(); ++j)
         cout << " " << i->second[j];
      cout << endl;
   }

    return 0;
}



Sorted already, by the design of std::map.

> (It took me 3 minutes to take Aahz's script and change it, and I had one
> bug: forgot to import sys and string)
>
> In 1 more minute, it is possible to change the definition of a word to "a
> consecutive alphabetic characters".
1 minute or less.
In 30 more seconds, it is possible to
> make strings case insensitive,
the same.

> and 30 more seconds to disregard "_"s.
If I understood that phrase, I could answer it.

>
> How much time would it take in C++?
>
> Remember that map<string, vector<int>> is invalid.
Are you making up rules here? Of course you meant map<string, vector<int> >.


> --
> Moshe Zadka <moshez at math.huji.ac.il>
> http://www.oreilly.com/news/prescod_0300.html
> http://www.linux.org.il -- we put the penguin in .com
>
>





More information about the Python-list mailing list