Java 'Prototype' pattern - new vs clone vs class.newInstance

前端 未结 6 1004
别那么骄傲
别那么骄傲 2021-02-08 18:11

In my project there are some \'Prototype\' factories that create instances by cloning a final private instance.

The author of those factories says that this pattern prov

6条回答
  •  旧时难觅i
    2021-02-08 18:48

    My tests with DecimalFormat (OpenJDK 8/Windows/JMH 1.19) shows completely opposite image:

    Benchmark               Mode  Cnt     Score     Error  Units
    Format.everyTimeCreate  avgt   10  9326.967 ± 199.511  us/op
    Format.useClone         avgt   10  5102.397 ±  72.993  us/op
    Format.useGlobalWithTL  avgt   10  4605.604 ±  59.000  us/op
    

    Looks like it is not so simple to answer what is perform better.

提交回复
热议问题