Skip to main content
added 10 characters in body; edited tags; edited title
Source Link

Too slow java method How to speed up a loop that does string manipulation in Java?

HaveI have a problemprogram that Oblig1.toString arebuilds a string in a loop, and my program is too slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7run Oblig1Test.oppgave7. Someone who knows what whoWhat could be done to speed it up?

Oblig1.toStringOblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}

Too slow java method

Have a problem that Oblig1.toString are too slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7. Someone who knows what who could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}

How to speed up a loop that does string manipulation in Java?

I have a program that builds a string in a loop, and my program is too slow. It takes now about 600 milliseconds to run Oblig1Test.oppgave7. What could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}
Post Closed as "Not suitable for this site" by bmargulies, Mark Rotteveel, Cole Tobin, Gordon Seidoh Worley, fotanus

To Too slow java method

Have a problem that Oblig1.toString are totoo slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7. Someone who knows what who could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}

To slow java method

Have a problem that Oblig1.toString are to slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7. Someone who knows what who could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}

Too slow java method

Have a problem that Oblig1.toString are too slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7. Someone who knows what who could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}
Source Link
mv700
  • 35
  • 1
  • 8

To slow java method

Have a problem that Oblig1.toString are to slow. It takes now about 600 milliseconds to do Oblig1Test.oppgave7. Someone who knows what who could be done to speed it up?

Oblig1.toString:

public static String toString(int[] a, char v, char h, String mellomrom)
{
    String s ="";
       
    s += v;
    
    if(a.length != 0)
    {
        for(int i = 0; i < a.length-1; i++)
        {
                s += a[i] + mellomrom; 
        }
        
        s += a[a.length-1];
    }
    
    s += h;
    
    return s;
}

Oblig1Test:

public static int oppgave7()
{
   int[] b = new int[20000];
   long tid = System.currentTimeMillis();
   Oblig1.toString(b,' ',' '," ");
   tid = System.currentTimeMillis() - tid;

  if (tid > 40)
  {
    System.out.println("Oppgave 7: Metoden "
      + "er for ineffektiv. Må forbedres!");
  }
}

public static void main(String[] args) throws IOException
{
   oppgave7();
}