Autor Beitrag
bms
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 735

Win XP
Delphi 3, Delphi 6 PE, Delphi 2005 PE
BeitragVerfasst: Sa 30.10.04 15:56 
Die könnte ich mir auch selbst programmieren, aber es gibt eine fertige Funktion. Nur fällt sie mich nicht mehr ein.

_________________
VORSICHT: Ich behersche seit heute die Fünf-Finger-Handflächen-Herz-Explodier-Schlag-Kampf-Technik >:(
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Sa 30.10.04 16:17 
ausblenden Delphi-Quelltext
1:
function Odd(X: Longint): Boolean;					

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Sa 30.10.04 18:54 
Möglicher weise geht es mit
ausblenden Delphi-Quelltext
1:
Zahl MOD 2					

schneller, weil da der Funktionsaufruf werfällt, wenn ihn der Delphicompiler nicht sowieso wegoptimiert.

_________________
Linux und OpenSource rulez!
Maweki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 197

Ubuntu Linux
Lazarus
BeitragVerfasst: Sa 30.10.04 19:37 
modulo is bekannterweise ein irre langsamer operator. Der schnellste weg ist doch, den letzten Bit einer ganzzahl auszulesen...
Also eher die endziffer zu pruefen als Modulo. Und sicherlich ist Odd nicht nur ein einfaches Modulo...
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: Sa 30.10.04 19:48 
Wie wärs mit
if (Zahl shr 1) shl 1 = Zahl then ...?

Weil selbst zwei Bitshifter sollten schneller sein als ein Modulo. Der Beweis bleibt dem Leser überlassen.

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
Maweki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 197

Ubuntu Linux
Lazarus
BeitragVerfasst: Sa 30.10.04 19:51 
na klar is das schneller. Ich meine Modulo fuehrt ja ne ganze Division durch...
Ich denke sogar, dass das ganze (fuer den Fall, dass es schneller is, als der Bitshifter) mitm InlineAssembler geloest wurde...
Wir koennen darauf vertrauen, dass die geschriebenen Sachen von Leuten gemacht wurden, die wissen, was sie tun. Daher wuerde ich mir fuer meinen Teil nie Anmassen, eine dieser Prozeduren und Funktionen selbst schneller oder genausogut zu schreiben...
Luzzifus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 200

Win2K
D6 Prof
BeitragVerfasst: Sa 30.10.04 20:49 
es geht noch einfacher:

ausblenden Delphi-Quelltext
1:
if (zahl and 1)=0 then zahl_ist_gerade;					

das tut in etwa das, was maweki gesagt hat: es prüft das 1-bit. da alle anderen 2er-potenzen gerade sind, repräsentiert genau dieses bit, ob die zahl gerade oder ungerade ist.


Zuletzt bearbeitet von Luzzifus am Sa 30.10.04 21:02, insgesamt 1-mal bearbeitet
fritierte
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 58



BeitragVerfasst: Sa 30.10.04 20:55 
wüe wärs mit
ausblenden Delphi-Quelltext
1:
ungerade = zahl and 1					

funktioniert doch, oder?

*edit*
hmm ok, ich werde nie wieder beim fernsehm was posten
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 30.10.04 22:06 
Ja, aber nur für Ungerade: Integer;

Ansonsten musst du wirklich die Prüfung auf = 0 durchführen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Maweki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 197

Ubuntu Linux
Lazarus
BeitragVerfasst: Sa 30.10.04 22:08 
was soll es sonst fuer ungerade geben ausser Integer?
bms Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 735

Win XP
Delphi 3, Delphi 6 PE, Delphi 2005 PE
BeitragVerfasst: Sa 30.10.04 23:02 
Nichts für Ungut Leute, aber Funktionen sind dazu da um benutzt zu werden. Wir schreiben hier kein Spiel wo ich eine million Prüfungen pro Sekunde machen muß. ODD reicht ;)

_________________
VORSICHT: Ich behersche seit heute die Fünf-Finger-Handflächen-Herz-Explodier-Schlag-Kampf-Technik >:(
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: So 31.10.04 02:56 
Maweki,
z.b. real single float double etc.
da kann man net auf die bits prüfen weil das anders gespeichert wird
da muss man mod nehmen
ScorpionKing
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1150

Win XP

BeitragVerfasst: So 31.10.04 07:43 
ich hat mal eine funktion (für c++) die ausrechnet ob die übergebene zahl eine primzahl ist. wenn ich die funtkion gefunden hab, poste ich sie mal!

_________________
Aus dem Urlaub zurück!
Luzzifus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 200

Win2K
D6 Prof
BeitragVerfasst: So 31.10.04 09:19 
Zitat:
Maweki,
z.b. real single float double etc.
da kann man net auf die bits prüfen weil das anders gespeichert wird
da muss man mod nehmen

allerdings stellt sich die frage, ob die zahl gerade oder ungerade ist, bei float typen sehr sehr selten :shock: :lol:

ausserdem kann man sie dann erst auf nen integer typ reduzieren (z.b. mit trunc) und dann das 1-bit prüfen. geht glaub ich auch noch schneller als modulo :wink:

gibts in delphi eigentlich direkte bit-prüfen / bit-setzen operatoren, wie BTR / BTS in assembler?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.10.04 09:53 
1. MOD geht NUR für Integer
2. FPU-Operationen sind lahm!
3. Ich kennen keinen normalen Programmierer, der nen Real auf Gerade\Ungerade überprüfen wöllte
4. Der InlineASM von Delphi unterstützt alle Grundlegenden i386-Befehle, ab D6 auch die 3dNow und MMX-Erweiterungen
5. Prüfung in ASM auf "gerader Vorkomma-Anteil":

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
asm
FLD1
FLD1
FADD ST, ST
FLD Number
FPREM
FCOMP ST, ST(1)
FSTSW AX
SAHF
MOV AX, 0 //Flags nicht verändern
JB @@Gerade
Ungerade:
INC AX
Gerade:
and EAX, 1

6. Den unter 5. aufgeführten Src würde auch kein Normalo verwenden!

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Luzzifus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 200

Win2K
D6 Prof
BeitragVerfasst: So 31.10.04 09:58 
6 Punkte, die unser aller Leben verändern werden!! :flehan:
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.10.04 10:45 
@Luzzifus:
Lass mir doch nicht immer SO VIEL Ehre zukommen :oops:

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: So 31.10.04 14:27 
@ScorpionKing: was hat das mit der ursprünglichen Frage zu tun? :?

@all: Odd ist keine Funktion in dem Sinn, der Compiler übersetzt sie nämlich mit genau einer ASM-Anweisung:
ausblenden Quelltext
1:
  test al, $01					

Wie Maweki schon sagte..
Maweki hat folgendes geschrieben:
Wir koennen darauf vertrauen, dass die geschriebenen Sachen von Leuten gemacht wurden, die wissen, was sie tun.

Genauso ist auch Assigned() (und viele andre Funktionen) eigentlich keine Funktion, sondern alles "Compiler-Magic"..!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
ScorpionKing
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1150

Win XP

BeitragVerfasst: Mi 24.11.04 13:18 
@Motzi: sorry, da hab ich wohl was falsches gepostet. ich meinte natürlich ich hätte eine funktion die das ausrechnet! :wink:
hab sie aber verloren, weil meine festplatte vor ungefähr 1 monat einen totalcrash hatte und seitdem nicht mehr funzt. hab jezt ne neue!

aber zum thema: warum ist mod (zahl mod 2) langsam. ist das nicht assembler?

_________________
Aus dem Urlaub zurück!
IngoD7
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 629


D7
BeitragVerfasst: Mi 24.11.04 13:51 
Motzi hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
function Odd(X: Longint): Boolean;					


Genau genommen stellt die Funktion Odd ja fest, ob eine Zahl ungerade ist. :lol:

Aber wie ein Thread, dessen klare Frage im ersten und dessen (fast) klare Antwort im zweiten Beitrag steht, noch auf 20 Postings kommen kann ... :roll: :?: