++ -- += -= ...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

++ -- += -= ...

TedVanGaalen
Hi there.

I am probably not the first, and will not be
the last person to come up
with this suggestion, neverTheLess:

To increment a numeric value I would like to write:

aNum := 1.
aNum ++.             to increment it
aNum --.             to decrement it

instead of:
aNum := aNum + 1.

For reasons of efficiency and readability.

Why?
Most, if not all, of today's computer processors
( they used to be called microprocessors :o)
have INCrement en DECrement instructions,
which are much more efficient than addition
and subtraction instructions.
(less clock cycles, perhaps even just one)

Of course, the work should to be done by primitives,
which directly utilizes the processor's INC, DEC instructions.

So, wouldn't it be convenient to implement these ++ and ---
methods in Pharo? and VM.  I am not sure, but I suspect also a number
of system classes e.g. in     do: [:i|...]   would perform better,
wenn this method/ instruction is supported?

?

Kind Regards
Ted
Currently making/learning Pharo/Seaside app.

Reply | Threaded
Open this post in threaded view
|

Re: ++ -- += -= ...

Tobias Pape
Hi,

Am 2011-04-16 um 13:32 schrieb Ted F.A. van Gaalen:

> Hi there.
>
> I am probably not the first, and will not be
> the last person to come up
> with this suggestion, neverTheLess:
>
> To increment a numeric value I would like to write:
>
> aNum := 1.
> aNum ++.             to increment it
> aNum --.             to decrement it
>
> instead of:
> aNum := aNum + 1.
>
> For reasons of efficiency and readability.
>
> Why?
> Most, if not all, of today's computer processors
> ( they used to be called microprocessors :o)
> have INCrement en DECrement instructions,
> which are much more efficient than addition
> and subtraction instructions.
> (less clock cycles, perhaps even just one)
>

Interestingly, this is already done when using a JIT,
eg, the CogVM.

But what is more important is, that in Smalltalk, Integers
are unmodifiable.

There are different concepts:

in C,

i++

means,

"increase the number in the memory cell that i represents"

however, in Smalltalk,

i ++

would mean

"send the object that is stored in i the message #++"
And if a number, eg 42, is stored in i, then this would mean
"send 42 the message #++"
but what should the result be?
should 42 transform itself into 43?
Moreover, it is bad style in Object-Oriented programming to
rely on side effects of a message:
You're implicitly saying
"I want the state of the object in i to change and rely on it
later-on"

whereas
i := i + 1
is explicitly saying

"I want a number greater than i and work with it later-on"

> Of course, the work should to be done by primitives,
> which directly utilizes the processor's INC, DEC instructions.
>
As I said this is subject to a JIT.

> So, wouldn't it be convenient to implement these ++ and ---
> methods in Pharo? and VM.  I am not sure, but I suspect also a number
> of system classes e.g. in     do: [:i|...]   would perform better,
> wenn this method/ instruction is supported?
>

I don't think so.

So Long,
        -Tobias

Reply | Threaded
Open this post in threaded view
|

Re: ++ -- += -= ...

Andres Valloud-4
In reply to this post by TedVanGaalen
64 bit x86 processors got rid of INC/DEC in favor of efficiency of other
instructions and in favor of adding the REX prefix...

On 4/16/11 4:32 , Ted F.A. van Gaalen wrote:

> Hi there.
>
> I am probably not the first, and will not be
> the last person to come up
> with this suggestion, neverTheLess:
>
> To increment a numeric value I would like to write:
>
> aNum := 1.
> aNum ++.             to increment it
> aNum --.             to decrement it
>
> instead of:
> aNum := aNum + 1.
>
> For reasons of efficiency and readability.
>
> Why?
> Most, if not all, of today's computer processors
> ( they used to be called microprocessors :o)
> have INCrement en DECrement instructions,
> which are much more efficient than addition
> and subtraction instructions.
> (less clock cycles, perhaps even just one)
>
> Of course, the work should to be done by primitives,
> which directly utilizes the processor's INC, DEC instructions.
>
> So, wouldn't it be convenient to implement these ++ and ---
> methods in Pharo? and VM.  I am not sure, but I suspect also a number
> of system classes e.g. in     do: [:i|...]   would perform better,
> wenn this method/ instruction is supported?
>
> ?
>
> Kind Regards
> Ted
> Currently making/learning Pharo/Seaside app.
>
> .
>

Reply | Threaded
Open this post in threaded view
|

Re: ++ -- += -= ...

Igor Stasenko
In reply to this post by TedVanGaalen
On 16 April 2011 13:32, Ted F.A. van Gaalen <[hidden email]> wrote:

> Hi there.
>
> I am probably not the first, and will not be
> the last person to come up
> with this suggestion, neverTheLess:
>
> To increment a numeric value I would like to write:
>
> aNum := 1.
> aNum ++.             to increment it
> aNum --.             to decrement it
>
this is wrong smalltalk syntax.
++ and -- are binary messages, which means you should pass an argument
to message, i.e:

num ++ x
num -- y.

If you want unary messages, use word:

num increment
num decrement

unless of course you want to extend the number of special operators,
which are only:
':=' for assignment
and '^' for return.

So, you wanna increase the number of syntax rules to either introduce
new operators (++/--) or
add reserved words (increment/decrement). But if we follow that road,
then next day one would argue why we don't have operators like
+=, *=, -= and so on.. And then smalltalk will slowly turn into C
dialect, where all language semantics available through syntax,
instead through implementation & message sends.

Smalltalk having very small number of syntax rules comparing to other
high level languages. And this is one of its best characteristics ,
among the rest.
So i prefer it to be staying this way.

> instead of:
> aNum := aNum + 1.
>
> For reasons of efficiency and readability.
>
> Why?
> Most, if not all, of today's computer processors
> ( they used to be called microprocessors :o)
> have INCrement en DECrement instructions,
> which are much more efficient than addition
> and subtraction instructions.
> (less clock cycles, perhaps even just one)
>
> Of course, the work should to be done by primitives,
> which directly utilizes the processor's INC, DEC instructions.
>
> So, wouldn't it be convenient to implement these ++ and ---
> methods in Pharo? and VM.  I am not sure, but I suspect also a number
> of system classes e.g. in     do: [:i|...]   would perform better,
> wenn this method/ instruction is supported?
>
> ?
>
> Kind Regards
> Ted
> Currently making/learning Pharo/Seaside app.
>
>



--
Best regards,
Igor Stasenko AKA sig.