Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Compare generics (that are numbers) Date: Thu, 30 Jul 2020 17:00:11 +0200 Organization: Decebal Computing Lines: 48 Message-ID: <87mu3h59qc.fsf@munus.decebal.nl> References: <87k0ym73id.fsf@munus.decebal.nl> <87ft9a6u35.fsf@munus.decebal.nl> <87bljy6qu3.fsf@munus.decebal.nl> <87zh7h5ouj.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="f8d0a939acc3a275654d8d40652c9997"; logging-data="19226"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1971CsbUpDKTPp2/3SS9dCGG74/SUvArZ8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cancel-Lock: sha1:/q4oiJUBQeAD/s+qVvZ5pPlM1kc= sha1:jGJWRNhZCE8w7rklz6yMY+joeuE= Xref: csiph.com comp.lang.java.programmer:39603 Eric Sosman writes: > On 7/30/2020 5:33 AM, Cecil Westerhof wrote: >> Arne Vajhøj writes: >> >>> But if this is performance critical then why not just drop the >>> generic stuff and write specific code for the types? >> >> Because I want to show the differences between using >> Float/float/Integer/int and different implementations thereof. > > Is this a "practical problem," or a "teaching exercise?" If the > former, I'd be interested to know how it arises -- needing to compare > one arbitrary Number to another (BigDecimal vs. AtomicLong?) seems not > out of the question, but surely unusual. Lets lift a corner of the veil. ;-) It is in between. During a whiteboard session during a job interview I was asked to optimise something. It started with going from Float to Integer. (In this case that was possible.) The functionality of Integer was not used, so it was good to switch to int. But a further optimisation would be to use an int array. When back home I was curious and started implementing it. For readability I also tried int[number][2], but that did not work at all. I kept it in my program, because it is good to show the result in my opinion. Then I also tried int[2][number]. With 25 million records and 250 repeats I got: |-----------------------+--------+----------+---------+---------+---------+----------+----------+----------| | Type | Memory | Sys Time | Gen Min | Gen Max | Gen Avg | Calc Min | Calc Max | Calc Avg | |-----------------------+--------+----------+---------+---------+---------+----------+----------+----------| | Integer Class[number] | 3.8 GB | 34.0 | 5.33E3 | 1.11E4 | 7.65E3 | 367 | 656 | 447.60 | | int Class[number] | 2.3 GB | 15.5 | 2.53E3 | 4.64E3 | 3.44E3 | 202 | 340 | 267.40 | | int [2 * number] | 0.6 GB | 5.5 | 1.09E3 | 1.52E3 | 1.27E3 | 61 | 112 | 75.10 | | int [number][2] | 2.3 GB | 24.0 | 4.23E3 | 7.30E3 | 5.51E3 | 183 | 338 | 239.18 | | int [2][number] | 0.6 GB | 6.0 | 1.06E3 | 1.61E3 | 1.30E3 | 81 | 133 | 107.03 | |-----------------------+--------+----------+---------+---------+---------+----------+----------+----------| System time is in minutes. Other times in milliseconds. With Floats the savings on memory will be a lot less, because the overhead for objects is less. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof