Slow data processing
in
Programming Questions
•
1 year ago
Hi all,
I have piece of code which reads file generated by several gps loggers, takes coordinates and draws them on screen. I don't know why, but the code is slow as hell. Time needed to draw 1 frame (which holds about 300 - 400 small ellipses) is about 6 seconds (tested on different machines).
At first I fought that the size of file (~20 MB) is the problem so I've commented out file reading part at all and inserted some sample strings but that didn't helped at all.
Tried drawing on directly on screen instead of PGraphics buffer - any positive results.
Now I'm stuck... Even the first frame (which is empty) takes 1 second to render - but this coud be true remembering all initialization going on.
So the format of gps.txt file is:
- time:0
- 1,18,25123456,54123456,15,17
- 1,19,25123456,54123456,18,15
- ...
- time:n
- 1,18,25123456,54123456,15,17
- 1,19,25123456,54123456,18,15
where non timestamp line follows this
- TYPE,ID,XCOORD,YCOORD,SPEED,HEADING
and the code:
- BufferedReader gps_file;
- String gps_line;
- PGraphics buferis;
- //int frame_ct; // Frame counter for filename
- int fps;
- int frm;
- void setup(){
- buferis = createGraphics(800, 800, P2D);
- size(800, 800);
- smooth();
- background(0);
- noStroke();
- //gps_file = createReader("/Users/vaskas/Dropbox/Transport/Beginning/GPS/gps.txt");
- //frame_ct = 0;
- fps = 0;
- frm = 0;
- }
- void draw(){
- int test = 1;
- /* try {
- gps_line = gps_file.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- test = 0;
- }*/
- // Just sample strings for test purposes
- if ((frm%350) == 0) {
- gps_line = "time: 5";
- } else {
- gps_line = "2,12,25100000, 54400000,12,27";
- }
- if (test == 0) {
- noLoop();
- } else {
- String[] laikas = match(gps_line, "time"); // Looking for timestamp line
- if (laikas != null) {
- // if timestamp found - draw entire thing to screen
- buferis.endDraw();
- image(buferis, 0, 0);
- // just for benchmark purposes
- println("Frame render time: " + ((millis() - fps)/1000.0) + "s");
- fps = millis();
- // Save a frame to disk
- //String fname = "transport-" + trim(nfs(frame_ct, 4))+".jpg";
- //save(fname);
- //frame_ct = frame_ct + 1;
- // Prepare for the new frame
- buferis.beginDraw();
- buferis.smooth();
- buferis.noStroke();
- buferis.background(0);
- } else {
- // If the line is the coordinates line
- // Split it apart
- String[] gps_data = split(gps_line, ',');
- // Choose color by vehicle type
- if (int(gps_data[0]) == 2) {
- buferis.fill(64,128,255);
- } else {
- buferis.fill(255,128,64);
- }
- // Convert GPS coords to screen coordinates and draw
- int x = (800 * (int(gps_data[2]) - 25000000)) / 300000;
- int y = 800 - (800 * (int(gps_data[3]) - 54300000)) / 200000;
- buferis.ellipse (x, y, 5, 5);
- }
- }
- frm = frm + 1;
- }
Could anyone point out my mistake?
Thank you.
Andrius
1