Recent

Author Topic: Sort Method for 5 var's type Integer  (Read 10416 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Sort Method for 5 var's type Integer
« Reply #15 on: January 05, 2018, 12:16:51 pm »
Nothing like being lazy.
I totally agree. Have a look at this:
Code: Pascal  [Select][+][-]
  1. program sortarray;
  2. {$mode delphi}{$MACRO ON}{$IF FPC_FULLVERSION < 30101}{$ERROR NEEDS FPC 3.1.1 or HIGHER}{$ENDIF}
  3. uses generics.collections;
  4. var
  5.   a:TArray<integer>;
  6.   i:integer;
  7. begin
  8.   a :=[3,1,2,5,4];
  9.   TArrayHelper<integer>.Sort(a);  // NOTE: This is a class. Not a type helper. Will be renamed/added to Tarray<T> in a future version.
  10.   for i in a do writeln(i);
  11. end.
:D
That's all (in trunk...) Quite a bit lazier, I guess...

Lazier is definitely better.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Sort Method for 5 var's type Integer
« Reply #16 on: January 05, 2018, 12:19:10 pm »
But that takes a separate download? Anyway you did a nice job! :D
Yes. I was in opposition to including rtl-generics into 3.0.4 (it was considered). So thanks to me we don't have rtl-generics in 3.0.4 and you need to download library on your own :).
Now we know whom to thank for that crime.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Sort Method for 5 var's type Integer
« Reply #17 on: January 05, 2018, 12:29:54 pm »
Test for "Nothing like being lazy"
Quote
Enter size of array (<=0 to end): 5
Enter max integer: 1000
Enter test count: 1000
SortBits elapsed: 31
SortGeneric elapsed: 16
--------------------
Enter size of array (<=0 to end): 50
Enter max integer: 1000
Enter test count: 1000
SortBits elapsed: 31
SortGeneric elapsed: 16
--------------------
Enter size of array (<=0 to end): 500
Enter max integer: 1000
Enter test count: 1000
SortBits elapsed: 47
SortGeneric elapsed: 171
--------------------
Enter size of array (<=0 to end): 5000
Enter max integer: 1000
Enter test count: 1000
SortBits elapsed: 94
SortGeneric elapsed: 1217
--------------------
Enter size of array (<=0 to end): 50000
Enter max integer: 1000
Enter test count: 1000
SortBits elapsed: 515
SortGeneric elapsed: 13946
--------------------
Why do we need to "Enter max integer"?
What if max is MaxLongint or MaxUIntValue (I hope you have enough memory to sort five numbers when one of them is close to the end of the range)?
How about negative numbers?

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Sort Method for 5 var's type Integer
« Reply #18 on: January 05, 2018, 12:33:55 pm »
I was in opposition to including rtl-generics into 3.0.4 (it was considered).
Why? (Due to the workarounds?)
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Sort Method for 5 var's type Integer
« Reply #19 on: January 05, 2018, 12:48:56 pm »
I was in opposition to including rtl-generics into 3.0.4 (it was considered).
Why? (Due to the workarounds?)
Yes, there is also more: FPC has critical bug for class var:

https://bugs.freepascal.org/view.php?id=28911

which is important for usage of case insensitive dictionaries:

https://bugs.freepascal.org/view.php?id=30433

for example:

Code: Pascal  [Select][+][-]
  1. StringMap := THashMap<String, TEmptyRecord>.Create(TIStringComparer.Ordinal);

the library should be perfect. There was also small problem with quadratic probing for TOpenAddressingQP<TKey, TValue> (kind of dictionary), which is anyway now fixed. Also was small bug for specific case in dictionaries (related to constref):

https://github.com/maciej-izak/generics.collections/blob/cf4e62eded46a03adce339ddf6552600c3e27d1b/tests/t1.lpr

also fixed (for now only in github version of library). I also need (or Sven) to fix few generics issues. I like to use anonymous methods in final version too. When you think about all of problems then you know that the better choice was not including rtl-generics to 3.0.4.
« Last Edit: January 05, 2018, 12:50:52 pm by hnb »
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Sort Method for 5 var's type Integer
« Reply #20 on: January 05, 2018, 06:12:46 pm »
see this:
https://github.com/bylaardt/TOrderList
like a boss style!

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Sort Method for 5 var's type Integer
« Reply #21 on: January 05, 2018, 06:41:19 pm »
see this:
https://github.com/bylaardt/TOrderList
like a boss style!

No, like overkill style. There a big difference between an array and a list. Your code is also the slowest for the problem presented (5 items) . KUDOS for parallel, zero for applicability.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Sort Method for 5 var's type Integer
« Reply #22 on: January 05, 2018, 06:49:51 pm »
Hi Thaddy (i miss the  >:D  >:D  >:D  >:D  thing and the "grumpy mode on")

I agree! 
too much for 5 items.

 

TinyPortal © 2005-2018