1. Fastest Way to...

I've got a 3-element sequence...

    { 3, -1, 0 }

and need to turn it into this

    { 1, -1, 0 }

That is, any positive values ( > 0 ) should be turned to one.

What's the fastest way to do this? Right now I'm using

    if pos[1] > 0 then
       pos[1] = 1
    end if
    if pos[2] > 0 then
       pos[2] = 1
    end if
    if pos[3] > 0 then
       pos[3] = 1
    end if

I have a feeling this is fastest. Will it be as fast if put in a loop 
for arbitrary length?

new topic     » topic index » view message » categorize

2. Re: Fastest Way to...

C. K. Lester wrote:

> I've got a 3-element sequence...
>
>    { 3, -1, 0 }
>
> and need to turn it into this
>
>    { 1, -1, 0 }
>
> That is, any positive values ( > 0 ) should be turned to one.


Okay, here's my attempt:

junk = pos <= 0
pos = pos * junk
pos += not junk

Yes, I know I could combine that into some big convoluted function, but 
you get the gist.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Fastest Way to...

----- Original Message ----- 
From: "C. K. Lester" <euphoric at cklester.com>
To: <EUforum at topica.com>
Subject: Fastest Way to...


> 
> 
> I've got a 3-element sequence...
> 
>     { 3, -1, 0 }
> 
> and need to turn it into this
> 
>     { 1, -1, 0 }
> 
> That is, any positive values ( > 0 ) should be turned to one.
> 
> What's the fastest way to do this? Right now I'm using
> 
>     if pos[1] > 0 then
>        pos[1] = 1
>     end if
>     if pos[2] > 0 then
>        pos[2] = 1
>     end if
>     if pos[3] > 0 then
>        pos[3] = 1
>     end if
> 
> I have a feeling this is fastest. Will it be as fast if put in a loop 
> for arbitrary length?
> 

Maybe this...

 pos = pos * (pos <= 0) + (pos > 0)

-- 
Derek

new topic     » goto parent     » topic index » view message » categorize

4. Re: Fastest Way to...

pos = { 3, -1, 0 }

mask = (pos > 0)
pos -= (pos * mask)
pos += mask

or

mask = (pos > 0)
pos *= (mask = 0)
pos += mask

        Lucius L. Hilley III

----- Original Message ----- 
From: "C. K. Lester" <euphoric at cklester.com>
To: <EUforum at topica.com>
Sent: Monday, October 27, 2003 09:24 PM
Subject: Fastest Way to...


> 
> 
> I've got a 3-element sequence...
> 
>     { 3, -1, 0 }
> 
> and need to turn it into this
> 
>     { 1, -1, 0 }
> 
> That is, any positive values ( > 0 ) should be turned to one.
> 
> What's the fastest way to do this? Right now I'm using
> 
>     if pos[1] > 0 then
>        pos[1] = 1
>     end if
>     if pos[2] > 0 then
>        pos[2] = 1
>     end if
>     if pos[3] > 0 then
>        pos[3] = 1
>     end if
> 
> I have a feeling this is fastest. Will it be as fast if put in a loop 
> for arbitrary length?
> 
> 
> 
> TOPICA - Start your own email discussion group. FREE!
> 
>

new topic     » goto parent     » topic index » view message » categorize

5. Re: Fastest Way to...

Derek Parnell wrote:

>>I've got a 3-element sequence...
>>
>>    { 3, -1, 0 }
>>
>>and need to turn it into this
>>
>>    { 1, -1, 0 }
>>
>>That is, any positive values ( > 0 ) should be turned to one.
>>
>>    
>Maybe this...
>
> pos = pos * (pos <= 0) + (pos > 0)
>  
>

Oh, sweet! I think my version coulda been that if I had stayed with it. :)

new topic     » goto parent     » topic index » view message » categorize

6. Re: Fastest Way to...

On Mon, 27 Oct 2003 21:09:04 -0600, "C. K. Lester"
<euphoric at cklester.com> wrote:

>
>
>C. K. Lester wrote:
>
>> I've got a 3-element sequence...
>>
>>    { 3, -1, 0 }
>>
>> and need to turn it into this
>>
>>    { 1, -1, 0 }
>>
>> That is, any positive values ( > 0 ) should be turned to one.
>
>
>Okay, here's my attempt:
>
>junk =3D pos <=3D 0
>pos =3D pos * junk
>pos +=3D not junk
>
>Yes, I know I could combine that into some big convoluted function, but=
=20
>you get the gist.

That's about 3=BD times slower.

In your first post, you "unrolled the loop" which is a classic way to
make something as fast as possible.

Pete

new topic     » goto parent     » topic index » view message » categorize

7. Re: Fastest Way to...

On Mon, 27 Oct 2003 22:12:41 -0600, "C. K. Lester"
<euphoric at cklester.com> wrote:

>Derek Parnell wrote:
>> pos = pos * (pos <= 0) + (pos > 0)
>
>Oh, sweet! I think my version coulda been that if I had stayed with it. :)

Maybe you had better play with this:

constant ipos={3,-1,0}
sequence pos
sequence mask
integer j
atom t
t=time()
for i=1 to 1000000 do
	pos=ipos
--============================
	if pos[1] > 0 then			-- 2.23
	   pos[1] = 1
	end if
	if pos[2] > 0 then
	   pos[2] = 1
	end if
	if pos[3] > 0 then
	   pos[3] = 1
	end if
--============================
--	mask=pos>0					-- 7.99
--	pos-=(pos*mask)
--	pos+=mask
--============================
--	mask=pos>0					-- 7.86
--	pos*=(mask=0)
--	pos+=mask
--============================
--	pos=pos*(pos<=0)+(pos>0)	-- 7.83
--============================
--	while 1 do					-- 5.86
--		j=find(1,pos>1)
--		if not j then exit end if
--		pos[j]=1
--	end while
--============================
--	for k=1 to length(pos) do	-- 2.68
--		if pos[k]>0 then pos[k]=1 end if
--	end for
end for

t=time()-t
?t
?pos

Those are my timings on the right for six different ways of doing
this. You may need another zero or two on the for loop blink

Pete

new topic     » goto parent     » topic index » view message » categorize

8. Re: Fastest Way to...

Pete Lomax wrote:

>>Okay, here's my attempt:
>>
>>junk =3D pos <=3D 0
>>pos =3D pos * junk
>>pos +=3D not junk
>>
>>    
>That's about 3 times slower.
>
>In your first post, you "unrolled the loop" which is a classic way to
>make something as fast as possible.
>
>Pete
>
>  
hey, Pete, thanks for that info. I later realized that it won't be a 
static size sequence, so I have to accommodate all sizes.

I bet looping through the sequence when length() is more than ? would be 
faster than the sequence operations... right?

new topic     » goto parent     » topic index » view message » categorize

9. Re: Fastest Way to...

Hello, can someone tell me what is wrong with this?  I'm using the hackserv
as a basis for a mud.  I've tried to change it to accomodate telnet, gmud,
zmud, etc..(basically any client) where it only supported telnet in the
beginning. I KNOW this is too much code..but i've just been adding stuff in
trying to make it work and nothing seems to work. I KNOW that I'm
overlooking something really easy..but I can't figure it out.  Can someone
tell me  if you know what is wrong with this?

 elsif action = FD_READ then   -- The client said somethin'
    junk = ""
    owork = ""
    while not match({CRLF},junk) do
      owork = WsockReadData(sock,1)
      if atom(owork) then
        exit
      end if
      if length(owork)<1 then
  exit
      end if
      if atom(owork) then
        exit
      end if
      if length(owork)<1 then
  exit
      end if
      junk = junk & owork

end while
    -- junk now contains the client's responce

    -- find this user in our list
    iwork = getUserFromSocket(sock)



 if length(players)>0 then
   for x=1 to length(players) do
 if players[x][1]=sock then
  charnum=x
 end if
   end for
  end if

players[charnum][6]=players[charnum][6]&junk

    if compare({10},players[charnum][6][(length(players[charnum][6]))]) or
compare({13},players[charnum][6][length(players[charnum][6])]) then

if compare({10},players[charnum][6][length(players[charnum][6])]) then

players[charnum][6]=players[charnum][6][1..(length(players[charnum][6])-1)]
end if

if compare({13},players[charnum][6][length(players[charnum][6])]) then

players[charnum][6]=players[charnum][6][1..(length(players[charnum][6])-1)]
end if

if compare({10},players[charnum][6][length(players[charnum][6])]) then

players[charnum][6]=players[charnum][6][1..(length(players[charnum][6])-1)]
end if
junk=players[charnum][6]

warnErr("junk:"&sprint(junk))

players[charnum][6]=""
call_proc(players[charnum][7],{sock,junk})
junk=""
end if


Thank you in advance,

Michelle



----- Original Message -----
From: "C. K. Lester" <euphoric at cklester.com>
To: <EUforum at topica.com>
Sent: Monday, October 27, 2003 11:34 PM
Subject: Re: Fastest Way to...


>
>
> Pete Lomax wrote:
>
> >>Okay, here's my attempt:
> >>
> >>junk =3D pos <=3D 0
> >>pos =3D pos * junk
> >>pos +=3D not junk
> >>
> >>
> >That's about 3 times slower.
> >
> >In your first post, you "unrolled the loop" which is a classic way to
> >make something as fast as possible.
> >
> >Pete
> >
> >
> hey, Pete, thanks for that info. I later realized that it won't be a
> static size sequence, so I have to accommodate all sizes.
>
> I bet looping through the sequence when length() is more than ? would be
> faster than the sequence operations... right?
>
>
>
> TOPICA - Start your own email discussion group. FREE!
>
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu