Autor Beitrag
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1652
Erhaltene Danke: 243

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Do 23.01.14 00:47 
Hallo,

ich dachte mir, Wurzel ziehen aus 2 mittels Heron Verfahren unter Einsatz von GMP müsste doch sehr simpel sein.
Mit freepascal unter linux64 ( da schwirrt ja libgmp sowieso herum, aber gpm.dll für Windows gibt es auch:
gmp-wrapper-for-delphi

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
//gespeichert unter sqrt2gmp.dpr
program sqrt2gmp;
{$IFdef fpc}
  {$MODE Delphi}
{$ELSE}
  {$APPTYPE CONSOLE}
{$ENDIF}

// Wurzel 2 im Heronverfahren für cDigits Dezimalstellen
// Ausgabe mp_printf funktionierte unter linux64 mit 32bitlibs nur bis 8192 Zeichen
// Unter Windows kein Problem
uses
  sysutils,
{$IFdef fpc}
    gmp;
{$ELSE}
  gmp_lib;
{$ENDIF}

const
 cDigits =100000;
var
  T2,T1,T0: TDateTime;
  n,cnt : integer;
  a,Naeherung,erg,die2: mpq_t;
  f : mpf_t;
  buf : string;
begin
  mpq_init(die2);
  mpq_init(a);
  mpq_init(Naeherung);
  mpq_init(erg);
  T0:= Time;
try
  mpq_set_str(die2,'10',2); // 2 im Dualsystem
  mpq_set(a,die2);
  mpq_set(Naeherung,die2);
  n := round(ln(cDigits*(2-sqrt(2))/sqrt(2))/ln(2)+1)+1;
  cnt := n;
  repeat
//   Naeherung := ( a / Naeherung+Naeherung) / die2 ;
    mpq_div(erg,a,Naeherung);           //  a/x
    mpq_add(Naeherung,erg,Naeherung);   //  a/x+x
    //Statt Mul shiften um n=1 Bit, ob das wirklich heutzutage noch schneller ist?
    mpq_div_2exp(Naeherung,Naeherung,1);// x = (a/x+x)/2
    dec(cnt);
  until cnt<0;
  //Jetzt in Fliesskommazahl umwandeln
  mpf_init2(f,round(cDigits/0.30103));// cDigits dezimalen  = 100000/ log10(2) = 33219?.???

  mpf_set_q(f,erg);

  T1:= Time;
  setlength(buf,cDigits+7);
  {$IFDEF FPC}
    mp_snprintf(PAnsiChar(buf),cDigits,'%.*Ff',cdigits,@f);
  {$else}
    gmp_printf(PAnsiChar(buf),'%.*Ff',cdigits,@f);
//    gmp_printf(PAnsiChar(buf), '%.*Ff ',@f,cDigits);
  {$endif}
  T2:= Time;
  writeln;
  writeln(buf);
  writeln( n,' Durchlaeufe');
  Writeln('Rechenzeit  ',FormatDateTime('NN:SS.ZZZ' ,T1-T0));
  Writeln('Ausgabezeit ',FormatDateTime('NN:SS.ZZZ' ,T2-T1));
finally
  mpf_clear(f);
  mpq_clear(erg);
  mpq_clear(Naeherung);
  mpq_clear(a);
  mpq_clear(die2);
  readln
end;
end.

der Rest der Ausgabe:
ausblenden Quelltext
1:
2:
3:
4:
...4012197700603576101524970318470624937068864601669535559159840183770081805610147523
17 Durchlaeufe
Rechenzeit  00:00.493
Ausgabezeit 00:00.158


Gruß Horst

Noch ein Edit, es gibt ja auch die Wurzelfunktion, noch wesentlich schneller siehe auch www.entwickler-ecke....ewtopic.php?t=109385 den Beitrag von user profile iconGammatester mparith oder hier mit gmp
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
..
  mpf_init2(f,round(cDigits/0.30103));
  mpfset_ui(f,2);
  mpf_sqrt(f,f);
..


Zuletzt bearbeitet von Horst_H am Di 28.01.14 14:09, insgesamt 2-mal bearbeitet

Für diesen Beitrag haben gedankt: Hochhaus
mandras
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 429
Erhaltene Danke: 107

Win 10
Delphi 6 Prof, Delphi 10.4 Prof
BeitragVerfasst: Do 23.01.14 00:58 
AMD Athlon II XE3 450 3,2GHZ Win 7 6GB Speicher
erster Start:5,226sec, zweiter: 5,241sec.

Für diesen Beitrag haben gedankt: Hochhaus
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 23.01.14 10:15 
AMD 64 X2 6000+, 2GB RAM => 5,687 sec.

Für diesen Beitrag haben gedankt: Hochhaus
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Do 23.01.14 14:02 
user profile iconHorst_H hat folgendes geschrieben Zum zitierten Posting springen:
Hallo,

ich teste nochmal meinen neuen i4330 3.5 Ghz 8 GB ( 1833 Mhz ? ) der braucht für 10000 Stellen 4,188 sec Linux/wine ( Pentomino ist unter wine schneller )
Haswell hin oder her, jetzt bin ich enttäuscht.

Gruß Horst


Mein Programm rechnet nicht nur sehr intensiv - es braucht auch den Hauptspeicher sehr intensiv. Was hast Du denn für Hauptspeicher eingebaut ?

Hochhaus
baumina
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 305
Erhaltene Danke: 61

Win 7
Delphi 10.2 Tokyo Enterprise
BeitragVerfasst: Do 23.01.14 14:21 
user profile iconHochhaus hat folgendes geschrieben Zum zitierten Posting springen:
user profile iconHorst_H hat folgendes geschrieben Zum zitierten Posting springen:
Hallo,

ich teste nochmal meinen neuen i4330 3.5 Ghz 8 GB ( 1833 Mhz ? ) der braucht für 10000 Stellen 4,188 sec Linux/wine ( Pentomino ist unter wine schneller )
Haswell hin oder her, jetzt bin ich enttäuscht.

Gruß Horst


Mein Programm rechnet nicht nur sehr intensiv - es braucht auch den Hauptspeicher sehr intensiv. Was hast Du denn für Hauptspeicher eingebaut ?

Hochhaus


Ich rate jetzt mal : 8 GB Arbeitsspeicher :lupe:

Oder meinst es handelt sich evtl. um Chinesenkram?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 23.01.14 14:44 
Zitat:
Ich rate jetzt mal : 8 GB Arbeitsspeicher :lupe:


Intensiv nutzen und viel nutzen ist nicht das gleiche ;) Die Zahl in Klammern könnte interessanter sein.
Wobei ich eher glaube das sich die Datenmengen in Grenzen halten sollte so das alles im CPU Cache passiert.
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Do 23.01.14 15:30 
8 Gb Hauptspeicher müsste problemlos reichen. Die Frage ist, wie sie getaktet sind - und mit dem Prozessor zusammenarbeiten. Das Programm nutzt ca. 10 - 15 MB Hauptspeicher - es passiert also nicht alles im Cache. Denke ich.

Hochhaus
papa69
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 79
Erhaltene Danke: 23

Win 10, Ubuntu
C#, Java, C
BeitragVerfasst: Do 23.01.14 15:36 
so, habe auch mal "getestet":

mehrere Versuche: meistens 5,741 sec
schnellstes Ergebnis nach 5,726 sec.

Prozessor: Inter Core 2 Quad8200
RAM: 6GB

Prozessor-Auslastung war ziemlich "gleichmäßig", wobei aber Kern 3 "mehr" zu tun hatte. Mittlere Auslastung lag bei ca.23% (Kern 3 bei ca.35%)

_________________
Daniel Bauer
... fatal ist nur, wenn sich das Licht am Ende des Tunnels als entgegenkommender ICE entpuppt ...

Für diesen Beitrag haben gedankt: Hochhaus
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1652
Erhaltene Danke: 243

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Do 23.01.14 16:45 
Hallo,

wie sieht denn der Quelltext aus?
Wie oben geschrieben, erreiche ich mit gmp 0.077 Sekunden für 100.000 Stellen, denn 10.000 kann ich mit time nicht messen.

Gruß Horst

Für diesen Beitrag haben gedankt: Hochhaus
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Do 23.01.14 16:59 
Das Programm ist sehr rechenintensiv, es ist kein ausgeklügelter Algorithmus.

Hochhaus


Code:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
Procedure TWurzForm.StartClick(Sender: TObject);
Var I, N, P, W: LongInt;
Begin

  Start.Enabled := False;
  ZeitX := GetTickCount;
  ZeitR.Text := '0,000 sec.';
  WurzEdit.Text := '';
  StelleX.Text := '0';
  Go := True;

  SetCursor(LoadCursor(0, PChar(idc_Wait)));
  Y := 'Wurzel.Doc';
  AssignFile(Fil, Y);

  Z := 0;
  Zahl    := 0.0;
  Stellen := 1000.0;
  Summe1  := 0;  Summe2   := 0;
  Q_Test  := 0;  Q_Wurzel := 0.0;

  For I := -1 To 2000000 Do Begin
    A[I] := 0;
  End{* For *}

  For I:=-2 To 4012000 Do Begin
    S[I] := 0;
    K[I] := 0;
  End{* For *}

  Val(Wurzel.Text, Zahl, R);
  Val(Stelle.Text, Stellen, R);

  N := Trunc(Stellen);

  If (Zahl <= 0.0Or (Zahl >= 1000Then Begin
   MessageDlg('Ungültige Zahl', mtError, [mbOk], 0);
   Stop.Enabled := False;
   Clear.Enabled := True;
   Exit;
  End;

  If (Stellen < 1Or (Stellen > 1000000Then Begin
   MessageDlg('Ungültige Anzahl Stellen', mtError, [mbOk], 0);
   Stop.Enabled := False;
   Clear.Enabled := True;
   Exit;
  End;

  P := Trunc(Sqrt(Zahl));

  K[-2] := Trunc((P * P)/100);
  K[-1] := Trunc((P * P)/10) - 10 * Trunc((P * P)/100);
  K[0]  := P * P - 10 * Trunc((P * P)/10);
  A[-1] := Trunc(P/10);
  A[0]  := P - 10 * Trunc(P/10);
  S[-2] := K[-2];
  S[-1] := K[-1];
  S[0]  := K[0];

  Q_Test   := 0;
  Q_Wurzel := 0.0;
  Q_Test   := Round(10000.0 * Zahl);

  If (Sqrt(Q_Test) = Trunc(Sqrt(Q_Test))) Then Begin
     Q_Wurzel := Sqrt(Q_Test)/100;
     Str(Q_Wurzel:2:4, StrZ);
     WurzEdit.Lines[0] := 'Unsinnige Vorgabe, QWurzel = ' +
     StrZ;
     Stop.Enabled := False;
     Clear.Enabled := True;
     Exit;
  End{* IF *}

  I := 1;  {* Wichtig: I Ist nach den obigen Schleifen nicht definiert *}


  {* Zentrale Schleife, die die Nachkommastellen mit Vektoroperationen *}
  {* berechnet. Jede Komponente ist eine Ziffer                        *}

  While (I <= N) And Go Do Begin

     Application.ProcessMessages;
     If (I Mod 100 = 0Then Begin
       ZeitY := GetTickCount - ZeitX;
       ZeitReal := ZeitY / 1000.0;
       ZeitR.Text := FloatToStrF(ZeitReal, ffFixed, 63) + ' sec.';
       Str(I, AnzStel);
       StelleX.Text := AnzStel;
     End;

     Stop.Enabled := True;
     Clear.Enabled := False;
     {* Ausmultiplizieren in Vektorschreibweise *}

     For W := -1 To I Do Begin
         Inc(K[W + I], 2 * A[W]);
     End;  {* For *}

     K[2 * I] := K[2 * I] + 1;


     {* Stellenübertrag *}

     For W := 2 * I DownTo -1 Do Begin
         Z := 0;
         While (K[W] > 9Do Begin
           Dec(K[W], 10);
           Inc(Z);
         End;
         Inc(K[W - 1], Z);
     End;  {* For *}


     {* 10'000 fache Eingabe und 10'000 fache Multiplikationssumme *}

     Summe1 := Round(1.0E06 * K[-2] + 1.0E05 * K[-1] + 1.0E04 * K[0] +
               1000.0 * K[1]);
     Summe1 := Summe1 + Round(100.0 * K[2] + 10.0 * K[3] + 1.0 * K[4]);
     Summe2 := Round(10000.0 * ZAHL);


     {* Grösse der Eingabe erreicht / Oder letzte Ziffer = 9 *}

     If (Summe1 >= Summe2) or (A[I] = 9Then Begin
       Inc(I);
       CopyMemory(@K[-2], @S[-2], 2 * I + 3);
     End {* If (Summe1 ... Then *}

     {* Sonst: Letzte Ziffer um 1 erhöhen *}

     Else Begin
       Inc(A[I]);
       CopyMemory(@S[-2], @K[-2], 2 * I + 3);
     End;  {* IF (Summe1 ... Else *}

  End;  {* While I <= N *}

  If (Not Go) Then N := I;
  ZeitY := GetTickCount - ZeitX;
  ZeitReal := ZeitY / 1000.0;

  If (Q_Wurzel = 0.0Then Begin
   ReWrite(Fil);
   Writeln(Fil,'Protokoll:');
   Writeln(Fil,'__________');
   Writeln(Fil,'          ');
   Writeln(Fil,'Quadratwurzel von ',Zahl:3:4,' auf ',N,' Stellen ist: ');
   Write(Fil, A[-1], A[0], '.');

   For I := 1 To N Do Begin
       If (I/72) = Trunc(I/72Then Writeln(Fil, ' ');
       Write(Fil, A[I]);
   End{* For *}

   Writeln(Fil, ' '); Writeln(Fil, ' ');
   Writeln(Fil, 'Ende des Protokolles.');
   CloseFile(Fil);
   ZeitR.Text := FloatToStrF(ZeitReal, ffFixed, 63) + ' sec.';
   WurzEdit.Lines.LoadFromFile(Y);

   Stop.Enabled := False;
   Clear.Enabled := True;

   SetCursor(LoadCursor(0, PChar(idc_Arrow)));
  End;
End;
D.Elphi
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 128
Erhaltene Danke: 10

Windows 10 Edu x64
Tokyo Starter, BlueJ
BeitragVerfasst: Do 23.01.14 18:33 
AMD FX-6100 (6x 3,3 GHz), 8 GB RAM: 5,156 Sekunden
MfG
D.Elphi

Für diesen Beitrag haben gedankt: Hochhaus
Jens01
Hält's aus hier
Beiträge: 9
Erhaltene Danke: 3



BeitragVerfasst: Do 23.01.14 21:25 
i7-4770k, 3,5GHz, 8GB-Ram, Win8-64
--> 3,500 sec

gruss Jens

Für diesen Beitrag haben gedankt: Hochhaus
D.Elphi
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 128
Erhaltene Danke: 10

Windows 10 Edu x64
Tokyo Starter, BlueJ
BeitragVerfasst: Do 23.01.14 22:15 
Irgendwie funktioniert das bei mir mit den Nachkommastellen nicht...
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Do 23.01.14 22:24 
user profile iconD.Elphi hat folgendes geschrieben Zum zitierten Posting springen:
Irgendwie funktioniert das bei mir mit den Nachkommastellen nicht...


Was funktioniert nicht ? Wenn ein Spruch kommt "Dateizugriff verweigert" muss das Programm in einem Verzeichnis laufen, das Schreibzugriff bietet.


Hochhaus
D.Elphi
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 128
Erhaltene Danke: 10

Windows 10 Edu x64
Tokyo Starter, BlueJ
BeitragVerfasst: Do 23.01.14 22:37 
Nein, ich bezog mich auf dieses Problem:
Bild
MfG
Einloggen, um Attachments anzusehen!

Für diesen Beitrag haben gedankt: Hochhaus
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Fr 24.01.14 08:15 
Ok. Das ist ein arger Bug. Ich werde ihn bald flicken. In der Schweiz gibt man einen Dezimalpunkt ein und KEIN Komma. Dann funktioniert alles normal.


Hochhaus
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Fr 24.01.14 08:52 
Mann ist mein System veraltet (Laptop Okt2006!)

CPU: T2300 auf 1,6 GHz (Dual Core, 32 Bit, "Yonah"
4 Gb Ram
Win 7 32bit

Zeiten:
a) 11,980 sec (dynamisch gedrosselte Leistung),
b) 10,921 sec (max Performance)
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 24.01.14 11:50 
Eigentlich kommt mir mein Notebook (hier auf der Arbeit) immer total träge vor, aber die Zeiten gehen ja: 4,586 sec (bestes: 4,508 sec - langsamstes: 4,712 sec - insg. ca. 10mal hintereinander getestet).

CPU: i5 (Dual Core, je 2,67 GHz)
RAM: 4 GB
Win 7 64bit
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Fr 24.01.14 13:52 
user profile iconTilo hat folgendes geschrieben Zum zitierten Posting springen:
Mann ist mein System veraltet (Laptop Okt2006!)
Oh, ich hab nicht den ältesten ;)

CPU: AMD Turion 64 X2 Mobile TL-58 (Tyler) (@1.9GHz)
RAM: 4 GB PC2-6400 (400 MHz)
Win XP 32bit

11,234 sec.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Hochhaus Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 662
Erhaltene Danke: 8

Windows 7
Delphi XE2
BeitragVerfasst: Fr 24.01.14 17:41 
user profile iconD.Elphi hat folgendes geschrieben Zum zitierten Posting springen:
Nein, ich bezog mich auf dieses Problem:
[Bild: Bild]
MfG


Müsste hiermit behoben sein ! Siehe Beilage.
Einloggen, um Attachments anzusehen!

Für diesen Beitrag haben gedankt: D.Elphi