From: Pascal Costanza on
On 20/07/2010 09:21, Pascal J. Bourguignon wrote:
On 20/07/2010 09:21, Pascal J. Bourguignon wrote:
> Pascal Costanza<pc(a)p-cos.net> writes:
>
>> On 20/07/2010 02:21, ansofive(a)gmail.com wrote:
>>> Success!
>>>
>>> In my googling I ran across the ext package in clisp - a quick review
>>> of loop and it's working. :)
>>>
>>> #+clisp
>>> (defun read-packet (stream)
>>> "Read a packet from the stream."
>>> (let ((buffer
>>> (loop for b = (read-byte stream)
>>> then (ext:read-byte-no-hang stream nil :eof)
>>> until (not b)
>>> collect b )))
>>> (make-array (length buffer) :element-type '(unsigned-byte
>>> 8) :initial-contents buffer)))
>>>
>>> Thanks again.
>>
>> Instead of
>>
>> (make-array (length bla)
>> :element-type '(unsigned-byte 8)
>> :initial-contents bla)
>>
>> you can also say
>>
>> (coerce bla '(vector (unsigned-byte 8)))
>>
>> which has a chance to be a bit more efficient.
>
> It would be a sad implementation, if it made any difference.
>


--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
> Pascal Costanza<pc(a)p-cos.net> writes:
>
>> On 20/07/2010 02:21, ansofive(a)gmail.com wrote:
>>> Success!
>>>
>>> In my googling I ran across the ext package in clisp - a quick review
>>> of loop and it's working. :)
>>>
>>> #+clisp
>>> (defun read-packet (stream)
>>> "Read a packet from the stream."
>>> (let ((buffer
>>> (loop for b = (read-byte stream)
>>> then (ext:read-byte-no-hang stream nil :eof)
>>> until (not b)
>>> collect b )))
>>> (make-array (length buffer) :element-type '(unsigned-byte
>>> 8) :initial-contents buffer)))
>>>
>>> Thanks again.
>>
>> Instead of
>>
>> (make-array (length bla)
>> :element-type '(unsigned-byte 8)
>> :initial-contents bla)
>>
>> you can also say
>>
>> (coerce bla '(vector (unsigned-byte 8)))
>>
>> which has a chance to be a bit more efficient.
>
> It would be a sad implementation, if it made any difference.

A first quick (and naive) test on clisp revealed that the coerce version
is actually a lot slower than the make-array version.

Weird, weird,

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: ansofive on
Hi,

> >> (coerce bla '(vector (unsigned-byte 8)))

Thanks for the hint.

The mindstorms brick only sends data at 2400bps so I'm not worried
about speed but it's always disconcerting when what you /know/ doesn't
jibe with what /is/.

Dennis
From: sds on
On Jul 20, 3:21 am, p...(a)informatimago.com (Pascal J. Bourguignon)
wrote:
> Pascal Costanza <p...(a)p-cos.net> writes:
> > Instead of
>
> > (make-array (length bla)
> >   :element-type '(unsigned-byte 8)
> >   :initial-contents bla)
>
> > you can also say
>
> > (coerce bla '(vector (unsigned-byte 8)))
>
> > which has a chance to be a bit more efficient.
>
> It would be a sad implementation, if it made any difference.

why? coerce is something quite complex, it can do many things,
while make-array is relatively simple.
I would expect make-array to be much faster.
From: Pascal J. Bourguignon on
sds <sam.steingold(a)gmail.com> writes:

> On Jul 20, 3:21�am, p...(a)informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Pascal Costanza <p...(a)p-cos.net> writes:
>> > Instead of
>>
>> > (make-array (length bla)
>> > � :element-type '(unsigned-byte 8)
>> > � :initial-contents bla)
>>
>> > you can also say
>>
>> > (coerce bla '(vector (unsigned-byte 8)))
>>
>> > which has a chance to be a bit more efficient.
>>
>> It would be a sad implementation, if it made any difference.
>
> why? coerce is something quite complex, it can do many things,
> while make-array is relatively simple.
> I would expect make-array to be much faster.

Because I expect dispatching on the type of bla, and on the target
type to be very small cost compared to the allocation of the vector
and copying of the data.

--
__Pascal Bourguignon__ http://www.informatimago.com/