java 关于equals 和intern 做比较哪个效率更高

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bingtunzi1300/article/details/89917210

这几天无事就研究了一下java虚拟机,在文章中看到String的intern也可以做字符串的比较,今天就来测一下,看哪个效率更高。

测试工具:JMH

运行环境:JDK8


@BenchmarkMode(Mode.Throughput) // 测试方法平均执行时间
@OutputTimeUnit(TimeUnit.MICROSECONDS) // 输出结果的时间粒度为微秒
public class TestJmh {

    public static void main(String[] args) throws RunnerException {
        Options build = new OptionsBuilder().include(TestJmh.class.getSimpleName()).forks(1).build();
        new Runner(build).run();
    }

    @Benchmark
    @Threads(10)
    public void equals(){
        String str1 = "我是医生";
        String str2 = new String("我是医生");
        boolean result = str1.equals(str2);
    }

    @Benchmark
    @Threads(10)
    public void intern(){
        String str1 = "我是医生";
        String str2 = new String("我是医生");
        str1 = str1.intern();
        str2 = str2.intern();
        boolean result = str1 == str2;
    }
}

OK 代码写好了,这个测试工具测试起来会费一些时间,大概在几分钟的样子,耐心等待就好

运行结果:

Result "com.sunny.jpa.jmh.TestJmh.equals":
3928.114 ±(99.9%) 1939.750 ops/us [Average]
(min, avg, max) = (3211.792, 3928.114, 4503.037), stdev = 503.747
CI (99.9%): [1988.364, 5867.864] (assumes normal distribution)

Result "com.sunny.jpa.jmh.TestJmh.intern":
  8.165 ±(99.9%) 1.410 ops/us [Average]
  (min, avg, max) = (7.611, 8.165, 8.577), stdev = 0.366
  CI (99.9%): [6.755, 9.575] (assumes normal distribution)

Benchmark        Mode  Cnt     Score     Error   Units
TestJmh.equals  thrpt    5  4623.603 ± 379.909  ops/us
TestJmh.intern  thrpt    5     8.548 ±   0.588  ops/us

看到这已经相当明显了,在实际应用中为何很少人推荐用这种字符串比较方式,因为在性能上查了不止一点点,intern在调用的时候会先去运行时常量池中寻找,如果找到则返回该字符串的引用,如果没找到则会把该字符串添加到常量池中,具体的时间耗费在哪一步操作目前没有深究,但是性能可见不止差了一点点哦,还是乖乖的使用equals哈

展开阅读全文

俩种写法哪个效率更高?

02-17

目的是为了查找俩个字符串里的最长公共部分rn[code=Delphi(Pascal)]rnfunction find(source: pchar; find: pchar): string;rnvarrn source_temp: PChar;rn pos, len, current_pos, pos_temp: Integer;rn pos_result, len_result: Integer;rn I: Integer;rnbeginrn source_temp := source;rn current_pos := 0;rn pos_temp :=0;rn pos_result := 0;rn len_result := 0;rnrn while (Ord(source_temp^) <> 0) dorn beginrn pos_temp := current_pos;rn for i := 0 to StrLen(find) dorn beginrn len := 0;rn if (source_temp^ = find[i]) thenrn beginrn pos := i;rn while ((source[pos_temp] = find[pos] ) and (Ord(source [pos_temp]) <> 0) and (Ord(find[pos]) <> 0)) dorn beginrn Inc(pos_temp);rn Inc(pos);rn Inc(len);rn end;rnrn if (len> len_result) thenrn beginrn len_result := len;rn pos_result := i;rn end;rn end;rn end;rn Inc(current_pos);rn Inc(source_temp);rn end;rnrn while(0 <>(len_result)) dorn beginrn Result := Result + find[pos_result];rn Inc(pos_result);;rn Dec(len_result);rn end;rnend;rnrn[/code]rn[code=Delphi(Pascal)]rnfunction commanstring(shortstring: pchar; longstring: pchar): string;rnvarrn J, I: Integer;rn substring: array[0..255] of Char;rnbeginrn result := '';rn j := 0;rnrn ShowMessage(IntToStr(Pos(shortstring, longstring)));rn if(Pos(shortstring, longstring) > 0) then //strstr是从大串中找小串,找到后返回子串rn Result := shortstring;rnrn for i := strlen(shortstring)-1 downto 1 do //否则,开始循环计算rn beginrn for j := 0 to strlen(shortstring) - i dorn beginrn FillChar(substring, SizeOf(substring), #0);rn Move(shortstring[j], substring[0], i);rn if (substring = 'Edit1') thenrn Null;rn if(Pos(substring, longstring) > 0) thenrn begin //strstr是从大串中找小串,找到后返回子串rn Result := substring;rn Exit;rn end;rn end;rn end;rnrnend;rnrn[/code] 论坛

没有更多推荐了,返回首页