Learn More
Learn More

Reputation: 1561

Optimizing String manupulation code

I am trying to all possible child domains from a given host. I have written following code. It works but my only worry is performance.

Is it required to optimize this code further:

import java.util.Arrays;
import java.util.Collections;

public class DemoExtractHostArray {
    public static  String[] createHostArray(String host) {
        String[] stringArr = host.split("\\.");
        String[] hostArray = new String[stringArr.length];
        int hostIndex = 0;

        for(int index = stringArr.length-1; index>=0;index--){
            if(hostIndex==0){ 
                hostArray[hostIndex] = stringArr[index];
            }
            else{
                hostArray[hostIndex] = stringArr[index]+"."+hostArray[hostIndex-1];
            }
            hostIndex++;
        }
        Collections.reverse(Arrays.asList(hostArray));
        return hostArray;
    }

    public static void main(String a[]){
        for(String s: createHostArray("a.b.c.d.e.f")){
            System.out.println(s);
        }

    }
}

Output:

a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
e.f
f

Upvotes: 2

Views: 124

Answers (3)

Manoj
Manoj

Reputation: 820

You can optimize the code like this

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {
    public static  String[] createHostArray(String host) {
        String[] stringArr = host.split("\\.");
        String[] hostArray = new String[stringArr.length];
        int hostIndex = 0;

        for(int index = stringArr.length-1; index>=0;index--){
            if(hostIndex==0){ 
                hostArray[hostIndex] = stringArr[index];
            }
            else{
                hostArray[hostIndex] = stringArr[index]+"."+hostArray[hostIndex-1];
            }
            hostIndex++;
        }
        Collections.reverse(Arrays.asList(hostArray));
        return hostArray;
    }

    public static  String[] betterCreateHostArray(String host) {
        List<String> hostList = new ArrayList<String>();
        do {
            if(!host.contains(".")) {
                hostList.add(host);
                break;
            } else {
                hostList.add(host);
                host = host.substring(host.indexOf('.')+1);
            }
        } while(host.length() > 0);

        return hostList.toArray(new String[hostList.size()]);
    }

    public static void main(String a[]){
        long startTime = System.nanoTime();
        String[] array = createHostArray("a.b.c.d.e.f");
        long endTime = System.nanoTime();
        long timeByFirstApproach = endTime - startTime;

        for(String s: array){
            System.out.println(s);
        }
        System.out.println("=====");
        startTime = System.nanoTime();
        array = betterCreateHostArray("a.b.c.d.e.f");
        endTime = System.nanoTime();
        long timeBySecondApproach = endTime - startTime;
        for(String s: array){
            System.out.println(s);
        }
        System.out.println(String.format("Time taken by first approach=[%d] nano seconds and\n"
                + "Time taken by second approach=[%d] nano seconds", timeByFirstApproach,timeBySecondApproach));
    }
}

and here the performance result

a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
e.f
f
=====
a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
e.f
f
Time taken by first approach=[1625572] nano seconds and Time taken by second approach=[308289] nano seconds
Second approach is more than 5 times faster than the approach you are following.

Upvotes: 0

75inchpianist
75inchpianist

Reputation: 4102

I would personally use recusion. This implementation does not require the reversal of the array and, in my opinion, may be easier to follow.

http://ideone.com/MnMZOL

package com.poachit.utility.web;

import java.util.Arrays;
import java.util.Collections;

public class DemoExtractHostArray {
     public static void createHostArray(String[] root, String[] result, int index) {

        String host="";
        int i = index;
        if (index == root.length) {
            return;
        }
        for ( ; i < root.length-1; i++) {           
            host += root[i] + ".";
        }

        if (i < root.length) {
            host += root[i];
        }

        result[index] = host;
        createHostArray(root, result, ++index);
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        String host = "a.b.c.d.e.f";

        String [] tokens = host.split("\\.");

        String [] result = new String[tokens.length];

      createHostArray(tokens, result, 0);

      for (String s : result) {
        System.out.println(s);
      }

    }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726909

The only potential improvement to your code is removing this call:

Collections.reverse(Arrays.asList(hostArray));

Since you are creating the hostArray and then reversing it, you might as well change the loop to create the array in reverse order right away, so as to no longer requiring an explicit reversal:

// hostIndex is no longer required - remove the line below:
// int hostIndex = 0;
for(int index = stringArr.length-1 ; index>=0 ; index--){
    if(index == stringArr.length-1) {
        hostArray[index] = stringArr[index];
    }
    else{
        hostArray[index] = stringArr[index]+"."+hostArray[index+1];
    }
}

Upvotes: 1

Related Questions