Seite 1 von 1

Ändern von Farben -Verschiedene Wege - Seltsamme Geschw.

Verfasst: 31.03.2009 01:13
von STARGÅTE
Tachchen,

der Titel wirk etwas seltsam aber es ist halt so.

Vorgabe: Eine RGB-Farbe soll in jedem Farbwert um 5 erhöht werden.

Dafür gibs ja verschiedene Wege:
- Normal, mit Red(), Green(), Blue(), RGB()
- Anders, mit <<, >> und & $FF
- Pointer, speicher direkt ändern

Code: Alles auswählen


Procedure Normal(Color, Add)
 Protected R, G, B
 R = Red(Color)   + Add
 G = Green(Color) + Add
 B = Blue(Color)  + Add
 ProcedureReturn RGB(R,G,B)
EndProcedure

Procedure Anders(Color, Add)
 Protected R, G, B
 R = Color & $FF + Add
 G = Color>>8 & $FF + Add
 B = Color>>16 & $FF  + Add
 ProcedureReturn R + G<<8 + B<<16
EndProcedure

  Structure Color
   R.c : G.c : B.c : A.c
  EndStructure
Procedure Pointer(Color, Add)
 Protected *Color.Color = @Color
 With *Color
  \R + Add
  \G + Add
  \B + Add
 EndWith
 ProcedureReturn Color
EndProcedure

#Size = 50000000

Time = ElapsedMilliseconds()
For n = 1 To #Size
 Normal($F0A050, 5)
Next
Time1 = ElapsedMilliseconds()-Time

Time = ElapsedMilliseconds()
For n = 1 To #Size
 Anders($F0A050, 5)
Next
Time2 = ElapsedMilliseconds()-Time

Time = ElapsedMilliseconds()
For n = 1 To #Size
 Pointer($F0A050, 5)
Next
Time3 = ElapsedMilliseconds()-Time

MessageRequester("Testergebnis", "Time1: "+Str(Time1)+Chr(10)+"Time2: "+Str(Time2)+Chr(10)+"Time3: "+Str(Time3))
Meine Ausgabe ist:
---------------------------
Testergebnis
---------------------------
Time1: 2187
Time2: 875
Time3: 2953
---------------------------
OK
---------------------------
Nun stellt sich mir die frage wieso die Pointer Variante am längst braucht ?
Das die Andere Variante mit <<,>>,& schneller ist als die Normale ist klar.
Aber warum ist eine direkte addition von 1 in jedem Character in der Farbe langsammer als wenn man erst noch Bitverschiebung usw. machen muss?

Verfasst: 31.03.2009 01:24
von ts-soft
> Nun stellt sich mir die frage wieso die Pointer Variante am längst braucht ?
Kann ich nicht bestätigen, aber die mit shift ist am schnellsten.

Verfasst: 31.03.2009 14:11
von KeyKon
Kann ich ebenfalls nicht bestätigen...
Time1: 1512
Time2: 671
Time3: 1152

Verfasst: 31.03.2009 14:21
von Kaeru Gaman
@KK
das sind deine Ergebnisse?

schon seltsam, dass ein doppeltes shift mit drei dummies so viel schneller ist als ein strukturierter zugriff.... *kopfkratz*


wirklich interessant wäre aber sowieso ein geschwindigkeitsvergleich mit der um- und rück-wandlung in HSV...
weil ne Menge Effekte grundsätzlich auf HSV angewendet werden.

Verfasst: 31.03.2009 14:23
von KeyKon
Kaeru Gaman hat geschrieben:@KK
das sind deine Ergebnisse?
Nein, die Werte hab ich mir grade ausgedacht -.-

Verfasst: 31.03.2009 15:05
von STARGÅTE
liegt es vllt nur daran das die Structure \R,\G,\B alle Characters sind, und er dadurch länger braucht, als wnen es LONGs wäre wie halt bei den andern ...

aber selbst wenn ich über all n .c hinschreiben bleiben die zeiten ähnlich ... natürlich ohne debugger ...

Verfasst: 31.03.2009 15:08
von Fluid Byte
---------------------------
Testergebnis
---------------------------
Time1: 1594
Time2: 531
Time3: 1172
---------------------------
OK
---------------------------

Verfasst: 31.03.2009 15:36
von DarkDragon

Code: Alles auswählen

---------------------------
Testergebnis
---------------------------
Time1: 1015
Time2: 422
Time3: 844
---------------------------
OK   
---------------------------
Hier mal der Assemblercode:

Code: Alles auswählen

; 
; PureBasic 4.30 (Windows - x86) generated code
; 
; (c) 2008 Fantaisie Software
; 
; The header must remain intact for Re-Assembly
; 
; String
; Requester
; FileSystem
; Date
; Object
; SimpleList
; Misc
; :System
; KERNEL32
; :Import
; 
format MS COFF
; 
extrn _ExitProcess@4
extrn _GetModuleHandleA@4
extrn _HeapCreate@12
extrn _HeapDestroy@4
; 
extrn PB_Blue
extrn _PB_ElapsedMilliseconds@0
extrn _PB_FreeSimpleLists@0
extrn PB_Green
extrn _PB_InitRequester@0
extrn _PB_InitSimpleList@0
extrn _PB_MessageRequester@8
extrn PB_Red
extrn PB_RGB
extrn _PB_Str@12
extrn _SYS_CopyString@0
extrn _memset
extrn _PB_StringBase
extrn PB_StringBase
extrn _SYS_InitString@0
; 
extrn _PB_StringBasePosition
public _PB_Instance
public _PB_ExecutableType
public _PB_MemoryBase
public PB_Instance
public PB_MemoryBase
public _PB_EndFunctions

macro pb_public symbol
{
  public  _#symbol
  public symbol
_#symbol:
symbol:
}

macro    pb_align value { rb (value-1) - ($-_PB_DataSection + value-1) mod value }
macro pb_bssalign value { rb (value-1) - ($-_PB_BSSSection  + value-1) mod value }
public PureBasicStart
; 
section '.code' code readable executable
; 
; 
PureBasicStart:
; 
  PUSH   dword I_BSSEnd-I_BSSStart
  PUSH   dword 0
  PUSH   dword I_BSSStart
  CALL  _memset
  ADD    esp,12
  PUSH   dword 0
  CALL  _GetModuleHandleA@4
  MOV    [_PB_Instance],eax
  PUSH   dword 0
  PUSH   dword 4096
  PUSH   dword 0
  CALL  _HeapCreate@12
  MOV    [PB_MemoryBase],eax
  CALL  _SYS_InitString@0
  CALL  _PB_InitSimpleList@0
  CALL  _PB_InitRequester@0
; :
; 
; 
; Procedure Normal(Color, Add)
macro MP0{
_Procedure0:
  PUSH   ebx
  PS0=20
  XOR    eax,eax
  PUSH   eax
  PUSH   eax
  PUSH   eax                                                                                                                                                                                              
; Protected R, G, B
; R = Red(Color)   + Add
  MOV    eax,dword [esp+PS0+0]
  CALL   PB_Red
  MOV    ebx,eax
  ADD    ebx,dword [esp+PS0+4]
  MOV    dword [esp],ebx
; G = Green(Color) + Add
  MOV    eax,dword [esp+PS0+0]
  CALL   PB_Green
  MOV    ebx,eax
  ADD    ebx,dword [esp+PS0+4]
  MOV    dword [esp+4],ebx
; B = Blue(Color)  + Add
  MOV    eax,dword [esp+PS0+0]
  CALL   PB_Blue
  MOV    ebx,eax
  ADD    ebx,dword [esp+PS0+4]
  MOV    dword [esp+8],ebx
; ProcedureReturn RGB(R,G,B)
  PUSH   dword [esp+8]
  PUSH   dword [esp+8]
  MOV    eax,dword [esp+8]
  CALL   PB_RGB
  JMP   _EndProcedure1
; EndProcedure
  XOR    eax,eax
_EndProcedure1:
  ADD    esp,12
  POP    ebx
  RET    8
}
; 
; Procedure Anders(Color, Add)
macro MP2{
_Procedure2:
  PUSH   ebx
  PUSH   edi
  PS2=24
  XOR    eax,eax
  PUSH   eax
  PUSH   eax
  PUSH   eax                                                                                                                                                                                    
; Protected R, G, B
; R = Color & $FF + Add
  MOV    ebx,dword [esp+PS2+0]
  AND    ebx,255
  ADD    ebx,dword [esp+PS2+4]
  MOV    dword [esp],ebx
; G = Color>>8 & $FF + Add
  MOV    ebx,dword [esp+PS2+0]
  SAR    ebx,8
  AND    ebx,255
  ADD    ebx,dword [esp+PS2+4]
  MOV    dword [esp+4],ebx
; B = Color>>16 & $FF  + Add
  MOV    ebx,dword [esp+PS2+0]
  SAR    ebx,16
  AND    ebx,255
  ADD    ebx,dword [esp+PS2+4]
  MOV    dword [esp+8],ebx
; ProcedureReturn R + G<<8 + B<<16
  MOV    ebx,dword [esp]
  MOV    edi,dword [esp+4]
  SAL    edi,8
  ADD    ebx,edi
  MOV    edi,dword [esp+8]
  SAL    edi,16
  ADD    ebx,edi
  MOV    eax,ebx
  JMP   _EndProcedure3
; EndProcedure
  XOR    eax,eax
_EndProcedure3:
  ADD    esp,12
  POP    edi
  POP    ebx
  RET    8
}
; 
; Structure Color
; R.c : G.c : B.c : A.c
; EndStructure
; Procedure Pointer(Color, Add)
macro MP4{
_Procedure4:
  PUSH   ebp
  PUSH   ebx
  PS4=16
  XOR    eax,eax
  PUSH   eax                                                                                                                                                                                                        
; Protected *Color.Color = @Color
  LEA    eax,[esp+PS4+0]
  MOV    dword [esp],eax
; With *Color
; \R + Add
  MOV    ebp,dword [esp]
  MOVZX  ebx,byte [ebp]
  ADD    ebx,dword [esp+PS4+4]
  PUSH   ebx
  MOV    ebp,dword [esp+4]
  POP    eax
  MOV    byte [ebp],al
; \G + Add
  MOV    ebp,dword [esp]
  MOVZX  ebx,byte [ebp+1]
  ADD    ebx,dword [esp+PS4+4]
  PUSH   ebx
  MOV    ebp,dword [esp+4]
  POP    eax
  MOV    byte [ebp+1],al
; \B + Add
  MOV    ebp,dword [esp]
  MOVZX  ebx,byte [ebp+2]
  ADD    ebx,dword [esp+PS4+4]
  PUSH   ebx
  MOV    ebp,dword [esp+4]
  POP    eax
  MOV    byte [ebp+2],al
; EndWith
; ProcedureReturn Color
  MOV    eax,dword [esp+PS4+0]
  JMP   _EndProcedure5
; EndProcedure
  XOR    eax,eax
_EndProcedure5:
  ADD    esp,4
  POP    ebx
  POP    ebp
  RET    8
}
; 
; #Size = 50000000
; 
; Time = ElapsedMilliseconds()
  CALL  _PB_ElapsedMilliseconds@0
  MOV    dword [v_Time],eax
; For n = 1 To #Size
  MOV    dword [v_n],1
_For1:
  MOV    eax,50000000
  CMP    eax,dword [v_n]
  JL    _Next2
; Normal($F0A050, 5)
  PUSH   dword 5
  PUSH   dword 15769680
  CALL  _Procedure0
; Next
_NextContinue2:
  INC    dword [v_n]
  JMP   _For1
_Next2:
; Time1 = ElapsedMilliseconds()-Time
  CALL  _PB_ElapsedMilliseconds@0
  MOV    ebx,eax
  SUB    ebx,dword [v_Time]
  MOV    dword [v_Time1],ebx
; 
; Time = ElapsedMilliseconds()
  CALL  _PB_ElapsedMilliseconds@0
  MOV    dword [v_Time],eax
; For n = 1 To #Size
  MOV    dword [v_n],1
_For3:
  MOV    eax,50000000
  CMP    eax,dword [v_n]
  JL    _Next4
; Anders($F0A050, 5)
  PUSH   dword 5
  PUSH   dword 15769680
  CALL  _Procedure2
; Next
_NextContinue4:
  INC    dword [v_n]
  JMP   _For3
_Next4:
; Time2 = ElapsedMilliseconds()-Time
  CALL  _PB_ElapsedMilliseconds@0
  MOV    ebx,eax
  SUB    ebx,dword [v_Time]
  MOV    dword [v_Time2],ebx
; 
; Time = ElapsedMilliseconds()
  CALL  _PB_ElapsedMilliseconds@0
  MOV    dword [v_Time],eax
; For n = 1 To #Size
  MOV    dword [v_n],1
_For5:
  MOV    eax,50000000
  CMP    eax,dword [v_n]
  JL    _Next6
; Pointer($F0A050, 5)
  PUSH   dword 5
  PUSH   dword 15769680
  CALL  _Procedure4
; Next
_NextContinue6:
  INC    dword [v_n]
  JMP   _For5
_Next6:
; Time3 = ElapsedMilliseconds()-Time
  CALL  _PB_ElapsedMilliseconds@0
  MOV    ebx,eax
  SUB    ebx,dword [v_Time]
  MOV    dword [v_Time3],ebx
; 
; MessageRequester("Testergebnis", "Time1: "+Str(Time1)+Chr(10)+"Time2: "+Str(Time2)+Chr(10)+"Time3: "+Str(Time3))
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  MOV    edx,_S1
  CALL  _SYS_CopyString@0
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  MOV    eax,dword [v_Time1]
  CDQ
  PUSH   edx
  PUSH   eax
  CALL  _PB_Str@12
  POP    eax
  MOV    edx,_S2
  CALL  _SYS_CopyString@0
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  MOV    eax,dword [v_Time2]
  CDQ
  PUSH   edx
  PUSH   eax
  CALL  _PB_Str@12
  POP    eax
  MOV    edx,_S3
  CALL  _SYS_CopyString@0
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  MOV    eax,dword [v_Time3]
  CDQ
  PUSH   edx
  PUSH   eax
  CALL  _PB_Str@12
  POP    eax
  INC    dword [_PB_StringBasePosition]
  PUSH   dword _S4
  MOV    edx,[PB_StringBase]
  ADD    [esp+4],edx
  CALL  _PB_MessageRequester@8
  POP    dword [_PB_StringBasePosition]
; 
_PB_EOP_NoValue:
  PUSH   dword 0
_PB_EOP:
  CALL  _PB_EndFunctions
  PUSH   dword [PB_MemoryBase]
  CALL  _HeapDestroy@4
  CALL  _ExitProcess@4
_PB_EndFunctions:
  CALL  _PB_FreeSimpleLists@0
  RET
; 
MP2
MP0
MP4
; 
section '.data' data readable writeable
; 
_PB_DataSection:
pb_public PB_DEBUGGER_LineNumber
  dd     -1
pb_public PB_DEBUGGER_IncludedFiles
  dd     0
pb_public PB_DEBUGGER_FileName
  db     0
_PB_ExecutableType: dd 0
public _SYS_StaticStringStart
_SYS_StaticStringStart:
_S1: db "Time1: ",0
_S3: db 10,"Time3: ",0
_S2: db 10,"Time2: ",0
_S4: db "Testergebnis",0
pb_public PB_NullString
  db     0
public _SYS_StaticStringEnd
_SYS_StaticStringEnd:
align 4
align 4
s_s:
  dd     0
  dd     -1
align 4
; 
section '.bss' readable writeable
_PB_BSSSection:
align 4
; 
I_BSSStart:
_PB_MemoryBase:
PB_MemoryBase: rd 1
_PB_Instance:
PB_Instance: rd 1
; 
align 4
PB_DataPointer rd 1
v_Time rd 1
v_n rd 1
v_Time1 rd 1
v_Time2 rd 1
v_Time3 rd 1
align 4
align 4
align 4
align 4
I_BSSEnd:
section '.data' data readable writeable
SYS_EndDataSection: