AVR32 - Java vs C++ flame
hoyuka
hoyuka at c2.hu
Sat Mar 11 20:55:12 CET 2006
Hali!
En is csinaltam egy tesztprogit, ennel nekem a C++ egy hajszallal gyorsabb
csak. A progi azt szamolja meg, hogy melyik unsigned short ertek fordul
elo leggyakrabban egy binaris fajlban:
///////////////// C++ //////////////////////
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
if (argc!=2) {
cerr<<"Usage: ShortCounter filename\n"; return 1;
}
ifstream file(argv[1]);
if (!file.is_open()) {
cerr<<"Failed to open file:"<<argv[1]<<endl; return 1;
}
int cntArray[65536];
unsigned short shortBuf[65536];
for(int i=0; i<65536; i++) cntArray[i]=0;
unsigned int cntMax=0; int mostOften=-1;
while (int read=file.readsome((char*)shortBuf, 65536*2)>>1) {
for(int i=0; i<read; i++) {
unsigned int shortValue=shortBuf[i];
unsigned int cnt=++cntArray[shortValue];
if (cnt>cntMax) { cntMax=cnt; (unsigned
int)mostOften=shortValue; }
}
}
file.close();
cout<<hex<<mostOften<<" occures in the file "<<dec<<cntMax<<" times.\n";
return 0;
}
///////////////// JAVA /////////////////////////
import java.io.*;
import java.io.FileInputStream;
class ShortCounter {
public static void main(String args[]) throws Exception {
if (args.length!=1) {
System.err.println("Usage: java ShortCounter filename\n");
System.exit(1);
}
String fName=args[0];
FileInputStream file=null;
try {
file=new FileInputStream(fName);
} catch (Exception e) {
System.err.println("Failed to open file:"+fName);
System.exit(1);
}
int cntArray[]=new int[65536];
byte byteBuf[]=new byte[65536*2];
for(int i=0; i<65536; i++) cntArray[i]=0;
int cntMax=0; int mostOften=0;
int read;
while ((read=file.read(byteBuf))>0) {
for(int i=0; i<read; i+=2) {
int lowByte=((int)byteBuf[i])&0xff;
int highByte=((int)byteBuf[i+1])&0xff;
int shortValue=lowByte|(highByte<<8);
int cnt=++cntArray[shortValue];
if (cnt>cntMax) {
cntMax=cnt; mostOften=shortValue;
}
}
}
file.close();
System.out.println(
Integer.toString(mostOften, 16)+" occures in the file
"+cntMax+" times."
);
System.exit(0);
}
}
//////////////////// Futasi idok ////////////////////////
$ time ./ShortCounter ~/install/jdk-1_5_0_06-linux-i586.bin
0 occures in the file 31339 times.
real 0m6.040s
user 0m5.573s
sys 0m0.305s
$ time java ShortCounter ~/install/jdk-1_5_0_06-linux-i586.bin
0 occures in the file 31339 times.
real 0m6.435s
user 0m5.792s
sys 0m0.426s
//////////////////////////////////////////////////////////
A JAVA mellett beszel az, hogy neki ossze kellett raknia a short-okat a
byte-okbol. Viszont ennel a peldanal nem feltetlenul kell futasidoben a
tombtulcimzest ellenorizni, mert forditasi idoben trivialis, hogy nem lesz
tulcimzes.
udv.
hoyuka
Auth Gábor said:
> Halihó!
>
> hoyuka ezt írta:
>> Most irogatok egy masik teszt progit.
>> Hogyan lehet JAVA-ban short tombot file-bol feltolteni
>> legegyszerubben? Csak olyan fuggvenyt lattam, ami byte[]-ba olvas be,
>> el megy vele kis ido, amig short-a konvertalom onnan az egyes
>> ertekeket.
>
> Hmm... szerintem nincs ilyen. Nem gyakori, hogy short-ba kellene
> olvasni... tippre olvass byte-ot és tedd short-ba.
> --
> http://www.enaplo.hu - Iskolai Információs Rendszer
> Auth Gábor -=- FreeBSD 5.4 -=- http://bsd.lap.hu
> +36-70/9400-341 -=- ICQ: 49179141 -=- Skype: authgabor
>
> -----------------------------------
> Szponzorunk: http://tonerbolt.hu/
>
--
http://hoyuka.myip.hu
-------------------------------------
Tele van a postaládája? C2MAIL 100 MB e-mail tárhely ingyen!
http://mail.c2.hu
More information about the Elektro
mailing list