Re: Python(2.5) reads an input file FASTER than pure C(Mingw)
"n00m" <[EMAIL PROTECTED]> schreef in bericht
news:[EMAIL PROTECTED]
import time
t=time.time()
f=open('D:\\some.txt','r')
z=f.readlines()
f.close()
print len(z)
print time.time()-t
m=input()
print z[m]
#include
#include
#include
#include
using namespace std;
char vs[1002000][99];
FILE *fp=fopen("D:\\some.txt","r");
int main() {
int i=0;
while (true) {
if (!fgets(vs[i],999,fp)) break;
++i;
}
first of all I would rewrite the C loop to:
int main() {
int i=0;
while (fgets(vs[i],999,fp))
++i;
}
but I think that the difference comes from what you do in the beginning of
the C source:
char vs[1002000][99];
this reserves 99,198,000 bytes so expect a lot of cache trashing in the C
code!
Is there an implementation of f.readlines on the internet somewhere?
interested to see in how they implemented it. I'm pretty sure they did it
smarter than just reserve 100meg of data :)
fclose(fp);
cout << i << endl;
cout << clock()/CLOCKS_PER_SEC << endl;
int m;
cin >> m;
cout << vs[m];
system("pause");
return 0;
}
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python(2.5) reads an input file FASTER than pure C(Mingw)
"SL" <[EMAIL PROTECTED]> schreef in bericht news:[EMAIL PROTECTED] "n00m" <[EMAIL PROTECTED]> schreef in bericht news:[EMAIL PROTECTED] using namespace std; char vs[1002000][99]; if (!fgets(vs[i],999,fp)) break; BTW why are you declaring the array as 99 and pass 999 to fgets to read a line? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python(2.5) reads an input file FASTER than pure C(Mingw)
Have you tried this now?
First try again with pure C code and compile with a C compiler, not
with C++ code and C++ compiler.
Then, tweak the code to use more buffering, to make it more similar
to readline code, like this (not tested):
#include
#include
char vs[1002000][100];
char buffer[65536];
int main(void) {
FILE *fp;
int i, m;
clock_t begin, end;
double t;
begin = clock();
fp = fopen("cvspython.txt", "r");
i = 0;
setvbuf(fp, buffer, _IOFBF, sizeof(buffer));
while(1) {
if(!fgets(vs[i], 100, fp)) break;
++i;
}
fclose(fp);
printf("%d\n", i);
end = clock();
t = (double)(end - begin)/CLOCKS_PER_SEC;
printf("%g\n", t);
scanf("%d", &m);
printf("%s\n", vs[m]);
getchar();
return 0;
}
Finally, repeat your statement again, if necessary.
--
http://mail.python.org/mailman/listinfo/python-list
computing with characters
How can I compute with the integer values of characters in python? Like 'a' + 1 equals 'b' etc -- http://mail.python.org/mailman/listinfo/python-list
Re: computing with characters
"Lutz Horn" <[EMAIL PROTECTED]> schreef in bericht
news:[EMAIL PROTECTED]
Hi,
2008/4/30 Gary Herron <[EMAIL PROTECTED]>:
SL wrote:
> How can I compute with the integer values of characters in python?
> Like 'a' + 1 equals 'b' etc
You can get an integer value from a character with the ord() function.
So just for completion, the solution is:
chr(ord('a') + 1)
'b'
thanks :) I'm a beginner and I was expecting this to be a member of string
so I couldnt find it anywhere in the docs.
--
http://mail.python.org/mailman/listinfo/python-list
Re: computing with characters
"Arnaud Delobelle" <[EMAIL PROTECTED]> schreef in bericht
news:[EMAIL PROTECTED]
"Gabriel Genellina" <[EMAIL PROTECTED]> writes:
En Wed, 30 Apr 2008 04:19:22 -0300, SL <[EMAIL PROTECTED]> escribió:
"Lutz Horn" <[EMAIL PROTECTED]> schreef in bericht
news:[EMAIL PROTECTED]
So just for completion, the solution is:
chr(ord('a') + 1)
'b'
thanks :) I'm a beginner and I was expecting this to be a member of
string so I couldnt find it anywhere in the docs.
And that's a very reasonable place to search; I think chr and ord are
builtin functions (and not str methods) just by an historical
accident. (Or is there any other reason? what's wrong with "a".ord()
or str.from_ordinal(65))?
Not a reason, but doesn't ord() word with unicode as well?
yes it does, I just read the documentation on it :)
--
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list
Re: computing with characters
"Gabriel Genellina" <[EMAIL PROTECTED]> schreef in bericht news:[EMAIL PROTECTED] En Wed, 30 Apr 2008 04:19:22 -0300, SL <[EMAIL PROTECTED]> escribió: And that's a very reasonable place to search; I think chr and ord are builtin functions (and not str methods) just by an historical accident. (Or is there any other reason? what's wrong with "a".ord() or str.from_ordinal(65))? yes when you know other OO languages you expect this. Anyone know why builtins were chosen? Just curious -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
file write question
I have written a script which:
- opens a file
- does what it needs to do, periodically writing to the file... for a
few hours
- then closes the file when it's done
So my question is:
Would it be better to 'open' and 'close' my file on each write cycle?
e.g.
def writeStuff(content):
myFile = open('aFile.txt', 'a+')
myFile.write(content)
myFile.close()
... or just leave it till it's done?
I don't need to use the file contents until the script is done
(although it would be nice... just to check it while it's working), so
just curious what people think is the better method.
- rd
--
http://mail.python.org/mailman/listinfo/python-list
Re: file write question
H - I am not familiar with flush(), will look into it. But an interesting note: I repeatedly and often start long running processes (one running right now: on about it's 14th hour), writing to open files, with few problems (on Mac OS X). Although of course I can't look at the results until the file closes...just have to hope it's right! LOL B - You are right about Sqlite, I'm a big fan (moved over from MySQL a few years ago). But the structure of the data for this project is much better suited to a 'flat file' format. Again, not that I am having problems, just thought I'd raise the topic. BTW PyCON is in Chicago this year (where I am), maybe I'll meet some of you there? RL -- http://mail.python.org/mailman/listinfo/python-list
