What makes my code use so much memory?

I solved in various ways a simple problem with CodeEval, the specification of which can be found here (just a few lines).

I created 3 working versions (one of them in Scala), and I do not understand the performance difference for my latest version of Java, which I expected to be the best time in terms of memory.

I also compared this to the code found on Github . Here are the performance statistics returned by CodeEval:

Stats

. Version 1 is the version found on Github, Version 2 is my Scala solution:

object Main extends App {
    val p = Pattern.compile("\\d+")
    scala.io.Source.fromFile(args(0)).getLines
        .filter(!_.isEmpty)
        .map(line => {
            val dists = new TreeSet[Int]
            val m     = p.matcher(line)
            while (m.find) dists += m.group.toInt

            val list  = dists.toList
            list.zip(0 +: list).map { case (x,y) => x - y }.mkString(",")
        })
        .foreach(println)
}

. Version 3 is my Java solution, which I expected to be the best:

public class Main {
    public static void main(String[] args) throws IOException {
        Pattern        p    = Pattern.compile("\\d+");
        File           file = new File(args[0]);
        BufferedReader br   = new BufferedReader(new FileReader(file));

        String line;
        while ((line = br.readLine()) != null) {
            Set<Integer> dists = new TreeSet<Integer>();
            Matcher      m     = p.matcher(line); 
            while (m.find()) dists.add(Integer.parseInt(m.group()));

            Iterator<Integer> it   = dists.iterator();
            int               prev = 0;
            StringBuilder     sb   = new StringBuilder();
            while (it.hasNext()) {
                int curr = it.next();
                sb.append(curr - prev);
                sb.append(it.hasNext() ? "," : "");
                prev = curr;
            }
            System.out.println(sb);
        }
        br.close();
    }
}
  • Version 4 is the same as version 3, except that I do not use StringBuilderoutput for printing and I do it as in version 1

:

  • 1 - System.out.print. , split ( ) .

  • 2 , - " " Scala CodeEval,

  • 2 , , . Scala, , Java,

  • 3 , . StringBuilder , mkString 2

  • 4 , System.out.println

- ?

+4
3

.

. java javascript. javascript :

random challenge

  • Rev 1: JS
  • . 2: .
  • . 3:

, , 200 5 . ! , codeevals , - (10 ).

, , : Random shot 2.

  • Rev4:
  • Rev5: . 8000 .: D

: . .

+3

scala , - " CodeEval", , TreeSet, . -

val regex = """\d+""".r // in the beginning, instead of your Pattern.compile
...
.map { line => 
    val dists = regex.findAllIn(line).map(_.toInt).toIndexedSeq.sorted
...

30-40% .

( , ), , " 3" (java- - ). , ( 50% , , , ). 600 , .

, , , Integeres, int dists[] = new int[600];.

, " ", . , , ints ( ), 20 000 ( 20K ) , , ... , .

+2

, , .

738 4513792 .

, , . .

import java.io.*;
import java.util.*;

public class Main {
public static void main (String[] args) throws IOException {
    File file = new File(args[0]);
    BufferedReader buffer = new BufferedReader(new FileReader(file));
    String line;
    while ((line = buffer.readLine()) != null) {
        line = line.trim();

        String out = new Main().getDistances(line); 

        System.out.println(out);
    }
}

public String getDistances(String s){

    //split the string
    String[] arr = s.split(";");

    //create an array to hold the distances as integers
    int[] distances = new int[arr.length];

    for(int i=0; i<arr.length; i++){
         //find the index of , - get the characters after that - convert to integer - add to distances array
        distances[i] = Integer.parseInt(arr[i].substring(arr[i].lastIndexOf(",")+1));    
    }

    //sort the array
    Arrays.sort(distances);

    String output = "";
    output += distances[0]; //append the distance to the closest city to the string

    for(int i=0; i<arr.length-1; i++){

        //get distance between current element(city) and next
        int distance_between = distances[i+1] - distances[i];

        //append the distance to the string
        output += "," + distance_between;
    }


    return output;   
 }
 }
+1
source

Source: https://habr.com/ru/post/1570119/


All Articles