It is currently Sat Nov 28, 2020 5:43 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Moving x number of bytes to another location
PostPosted: Sat Dec 21, 2013 9:56 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
Code:
  CopyMemory(codestrings(current_code)\cs(), @*outstream\byte[0]+*outstream\index, codestrings(current_code)\index)
  *outstream\index + codestrings(current_code)\index

I have a program that runs more slowly than I would like. I've traced the time consumption to these two lines of code. Basically all that's happening here is that I'm moving the contents of an array of bytes (type .a actually) to an indexed location in a block of allocated memory. Can anyone help me with an asm routine that would move the bytes more quickly than CopyMemory is currently doing it?

Here's a working snippet:
Code:
Structure decoder_table
  index.l
  Array cs.a(200)
EndStructure

Structure colorbits
  index.l
  byte.a[0]
EndStructure

Dim codestrings.decoder_table(0)
codestrings(0)\index = 150

*outstream.colorbits = AllocateMemory(1024)
*outstream\index = 100

; Move 150 bytes from codestrings array to outstream starting at *outstream\index
CopyMemory(codestrings(0)\cs(), @*outstream\byte[0]+*outstream\index, codestrings(0)\index)

Any hellep wilbert appreciated as idlelike to get this to go faster.

(sorry for my english I'm just learning)

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Moving x number of bytes to another location
PostPosted: Sun Dec 22, 2013 2:18 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
Ok, I was able to come up with this:
Code:
Procedure CopyPattern(*source, *dest, size)
  !cld
  !mov esi, [esp+4]
  !mov edi, [esp+8]
  !mov ecx, [esp+12]
  !rep movsb
EndProcedure

It works fine but it's exactly the same speed as CopyMemory :cry:

Is there a better way? Maybe with mmx? I don't know how to use that very well.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Moving x number of bytes to another location
PostPosted: Sun Dec 22, 2013 2:41 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 12, 2008 1:25 pm
Posts: 374
Location: The Netherlands
When I test the rep movsb method, I get a slower result than with CopyMemory.

CopyMemory: 328ms
rep movsb method: 877ms

That's with PB 5.21 LTS x86 with debugger off.

memcpy results in slightly faster code in my tests; ranging from 244ms to 294ms

Code:
Structure decoder_table
  index.l
  Array cs.a(200)
EndStructure

Structure colorbits
  index.l
  byte.a[0]
EndStructure

Dim codestrings.decoder_table(0)
codestrings(0)\index = 150

*outstream.colorbits = AllocateMemory(1024)
*outstream\index = 100

; Move 150 bytes from codestrings array to outstream starting at *outstream\index
#test_size = 9999999

s1 = ElapsedMilliseconds()
For i = 1 To #test_size
  CopyMemory(codestrings(0)\cs(), @*outstream\byte[0]+*outstream\index, codestrings(0)\index)
Next
e1 = ElapsedMilliseconds()

Procedure CopyPattern(*source, *dest, size)
  !cld
  !mov esi, [esp+4]
  !mov edi, [esp+8]
  !mov ecx, [esp+12]
  !rep movsb
EndProcedure

ImportC "" : memcpy.i (*dest, *src, sz) : EndImport

s2 = ElapsedMilliseconds()
For i = 1 To #test_size
  ;CopyPattern(codestrings(0)\cs(), @*outstream\byte[0]+*outstream\index, codestrings(0)\index)
  memcpy(@*outstream\byte[0]+*outstream\index, codestrings(0)\cs(), codestrings(0)\index)
Next
e2 = ElapsedMilliseconds()

OpenConsole()
PrintN("time1: " + Str(e1 - s1) + "ms")
PrintN("time2: " + Str(e2 - s2) + "ms")

ImportC ""
  system.i   (command.s)
EndImport

system("pause")

_________________
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!


Top
 Profile  
Reply with quote  
 Post subject: Re: Moving x number of bytes to another location
PostPosted: Sun Dec 22, 2013 6:54 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3706
Location: Netherlands
CopyMemory isn't that slow I think.
What would be the fastest approach depends on the cpu.
According to Thorium this would be the fastest on a Core i7
viewtopic.php?p=359802#p359802

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Moving x number of bytes to another location
PostPosted: Mon Jan 06, 2014 11:41 am 
Offline
Addict
Addict
User avatar

Joined: Sat Aug 15, 2009 6:59 pm
Posts: 1254
wilbert wrote:
CopyMemory isn't that slow I think.
What would be the fastest approach depends on the cpu.
According to Thorium this would be the fastest on a Core i7
viewtopic.php?p=359802#p359802

If a lot of small blocks are copied the push and pop need to be eliminated. They slow down the routine significantly if you are just copieing small blocks. Use a register instead.
Also the call itself may be the slow part, if you call it very often in a loop. So write the loop in Asm and inline the memory copy routine.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye