Skip to main content
edited tags; edited title; edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Loop performance Finding the array indices of three specific headers

deleted 12 characters in body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

I'm considering abouthow to refactor code to make it clean & clear.

There are some principles to make code clean; in this case I think it's Single Responsibility and One level of abstraction.

The original code is like thatthis:

for (int i = 0; i < header.length; i++) {
  if (header[i].equals(Constant.ID)) {
    indexOfID = i;
  } else if (header[i].equals(Constant.NAME)) {
    indexOfName = i;
  } else if (header[i].equals(Constant.CATEGORY)) {
    indexOfCategory = i;
  }
}

Then I seperateseparated the loop into smaller functions.

private int getIndexOfColumnType(String[] header, String type) {
  int index = -1;
  int i = 0;
  int size = header.length;

  while (index < size && index < 0) {
    if(header[i].equals(type)) {
      index = i;
    }
    i++;
  }
  return index;
}

And call it

int indexOfID = getIndexOfColumnType(header, Constant.ID);
int indexOfName = getIndexOfColumnType(header, Constant.NAME);
int indexOfCategory = getIndexOfColumnType(header, Constant.CATEGORY);

In this way, I need to use 3 loops; so I wonder which is better for performance because header array can be large.

Please tell me your idea.

Thank in advance.

I'm considering about refactor code to make it clean & clear.

There are some principles to make code clean; in this case I think it's Single Responsibility and One level of abstraction.

The original code like that:

for (int i = 0; i < header.length; i++) {
  if (header[i].equals(Constant.ID)) {
    indexOfID = i;
  } else if (header[i].equals(Constant.NAME)) {
    indexOfName = i;
  } else if (header[i].equals(Constant.CATEGORY)) {
    indexOfCategory = i;
  }
}

Then I seperate loop into smaller functions.

private int getIndexOfColumnType(String[] header, String type) {
  int index = -1;
  int i = 0;
  int size = header.length;

  while (index < size && index < 0) {
    if(header[i].equals(type)) {
      index = i;
    }
    i++;
  }
  return index;
}

And call it

int indexOfID = getIndexOfColumnType(header, Constant.ID);
int indexOfName = getIndexOfColumnType(header, Constant.NAME);
int indexOfCategory = getIndexOfColumnType(header, Constant.CATEGORY);

In this way, I need to use 3 loops; so I wonder which is better for performance because header array can be large.

Please tell me your idea.

Thank in advance.

I'm considering how to refactor code to make it clean & clear.

There are some principles to make code clean; in this case I think it's Single Responsibility and One level of abstraction.

The original code is like this:

for (int i = 0; i < header.length; i++) {
  if (header[i].equals(Constant.ID)) {
    indexOfID = i;
  } else if (header[i].equals(Constant.NAME)) {
    indexOfName = i;
  } else if (header[i].equals(Constant.CATEGORY)) {
    indexOfCategory = i;
  }
}

Then I separated the loop into smaller functions.

private int getIndexOfColumnType(String[] header, String type) {
  int index = -1;
  int i = 0;
  int size = header.length;

  while (index < size && index < 0) {
    if(header[i].equals(type)) {
      index = i;
    }
    i++;
  }
  return index;
}

And call it

int indexOfID = getIndexOfColumnType(header, Constant.ID);
int indexOfName = getIndexOfColumnType(header, Constant.NAME);
int indexOfCategory = getIndexOfColumnType(header, Constant.CATEGORY);

In this way, I need to use 3 loops; so I wonder which is better for performance because header array can be large.

Please tell me your idea.

Source Link
Manh Le
  • 117
  • 4

Loop performance

I'm considering about refactor code to make it clean & clear.

There are some principles to make code clean; in this case I think it's Single Responsibility and One level of abstraction.

The original code like that:

for (int i = 0; i < header.length; i++) {
  if (header[i].equals(Constant.ID)) {
    indexOfID = i;
  } else if (header[i].equals(Constant.NAME)) {
    indexOfName = i;
  } else if (header[i].equals(Constant.CATEGORY)) {
    indexOfCategory = i;
  }
}

Then I seperate loop into smaller functions.

private int getIndexOfColumnType(String[] header, String type) {
  int index = -1;
  int i = 0;
  int size = header.length;

  while (index < size && index < 0) {
    if(header[i].equals(type)) {
      index = i;
    }
    i++;
  }
  return index;
}

And call it

int indexOfID = getIndexOfColumnType(header, Constant.ID);
int indexOfName = getIndexOfColumnType(header, Constant.NAME);
int indexOfCategory = getIndexOfColumnType(header, Constant.CATEGORY);

In this way, I need to use 3 loops; so I wonder which is better for performance because header array can be large.

Please tell me your idea.

Thank in advance.