Programadores JAVA que não sabem OO vivem fazendo isso...
Autor: Blog do Dieval | Publicado: 2013-02-15T05:56:00-08:00

Veja o código que encontrei em uma aplicação open source famosa:

&nbsp; &nbsp; static double[] getValues(double[] source, int index) {
&nbsp; &nbsp; &nbsp; &nbsp; double[] result = new double[obj.size() - 1];
&nbsp; &nbsp; &nbsp; &nbsp; int index = obj.getIndex();
&nbsp; &nbsp; &nbsp; &nbsp; if (index == 0) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=1 ; i
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result[i-1] = values[i];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; } else {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index == result.length) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0 ; i
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i] = values[i];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0 ; i

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i] = values[i];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=index-1 ; i
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i] = values[i+1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }


Esse código é chamado milhares de vezes para um conjunto de registros... sendo que a maior&nbsp;frequência&nbsp;dos casos ocorre para a segunda condição.... me pergunto, qual a dificuldade em optar por duas soluções: 1) duplicar os dados sem o elemento do vetor desejado (no problema o valor do index é constante para cada conjunto de dados), ou 2) usar um pouco dos recursos da OO:


&nbsp; &nbsp; static interface ConvertArray {
&nbsp; &nbsp; &nbsp; &nbsp; double[] convert(double[] source, int ignoreIndex);
&nbsp; &nbsp; }

&nbsp; &nbsp; static class ClassFirstPos implements ConvertArray {
&nbsp; &nbsp; &nbsp; &nbsp; public double[] convert(double[] source, int ignoreIndex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Arrays.copyOfRange(source, 1, source.length);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; static class ClassLastPos implements ConvertArray {
&nbsp; &nbsp; &nbsp; &nbsp; public double[] convert(double[] source, int ignoreIndex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Arrays.copyOfRange(source, 0, source.length-1);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; static class ClassOtherPos implements ConvertArray {
&nbsp; &nbsp; &nbsp; &nbsp; public double[] convert(double[] source, int ignoreIndex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double[] result = new double[source.length - 1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(source, 0, result, 0, ignoreIndex - 1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(source, ignoreIndex, result, ignoreIndex - 1, result.length - ignoreIndex + 1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }



E para quem quiser testar as pequenas diferenças....


&nbsp; &nbsp; public static void main(String[] args) {
&nbsp; &nbsp; &nbsp; &nbsp; double[] values = new double[5000];
&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0 ; i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values[i] = (double)i+1;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; ConvertArray first = new ClassFirstPos();
&nbsp; &nbsp; &nbsp; &nbsp; ConvertArray last = new ClassLastPos();
&nbsp; &nbsp; &nbsp; &nbsp; ConvertArray other = new ClassOtherPos();

&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Original");
&nbsp; &nbsp; &nbsp; &nbsp; long t2 = System.nanoTime();
&nbsp; &nbsp; &nbsp; &nbsp; print( getValues(values,0) );
&nbsp; &nbsp; &nbsp; &nbsp; print( getValues(values,values.length) );
&nbsp; &nbsp; &nbsp; &nbsp; print( getValues(values,3) );
&nbsp; &nbsp; &nbsp; &nbsp; long t3 = System.nanoTime();
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(t3-t2);
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Versão OO");
&nbsp; &nbsp; &nbsp; &nbsp; long t0 = System.nanoTime();
&nbsp; &nbsp; &nbsp; &nbsp; print( first.convert(values, 0) );
&nbsp; &nbsp; &nbsp; &nbsp; print( last.convert(values, values.length) );
&nbsp; &nbsp; &nbsp; &nbsp; print( other.convert(values, 3) );
&nbsp; &nbsp; &nbsp; &nbsp; long t1 = System.nanoTime();
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(t1-t0);



}

Na média, a versão OO que demonstrei aqui tende a consumir 30% do tempo de processamento da versão original. Em vetores muito pequenos, pode ficar mais lento que a original, em função do custo do chamado das funções internas do método. Se for esse o caso, coloque os mesmos fors da versão original na versão OO e evite o uso dos IFs...