Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112844

Re: Warning: assignment from incompatible pointer type

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Warning: assignment from incompatible pointer type
Date 2017-06-22 20:08 +0200
Organization A noiseless patient Spider
Message-ID <oih0sr$2vg$1@dont-email.me> (permalink)
References (6 earlier) <oif9q6$s7i$2@dont-email.me> <lIM2B.59754$OJ2.17773@fx37.am4> <MaN2B.26007$3y7.25178@fx35.am4> <oigalp$lg8$1@dont-email.me> <aQR2B.15044$dH.11239@fx17.am4>

Show all headers | View raw


On 22/06/17 18:00, bartc wrote:
> On 22/06/2017 12:49, David Brown wrote:
>> On 22/06/17 12:42, bartc wrote:
> 
>>>>> from collections import Counter
>>>> msg = "Now is the time for all good men to come to the aid of their
>> country"
>>>>> m = msg * 1000000
>>>>> print(sorted(Counter(m).items()))
>>
>> The Pythonic way is "Find the right tool in the standard library.  Use
>> it to do the job - it will be faster than using hand-made methods".
>>
>> Testing on a faster computer with msg * 1000000:
>>
>> python 2.7 : 9.7 seconds
>> python 3.5 : 2.1 seconds
>> C : 0.1 seconds.
>>
>> pypy : 1.2 seconds
>>
>> So we see that the Python version takes just a couple of lines of clear
>> code (when written well),
> 
> I don't agree it is clear. The original required knowledge of .count(), 
> set(), and sorted(); this further needs .items() and Counter().

I think it is reasonable to expect a basic knowledge of Python here.  If 
you have used Python, you know what ".items()" does.  You might not have 
known of the "Counter" container type before - clearly DFS didn't or he 
would have used it.  But when you see "from collections import Counter" 
you can tell very quickly that it is a type of collection class, and its 
use is rather obvious from its name.  And as for "sorted" - if you can't 
guess what that does, you might want to start with some basic English 
lessons.

This is "clear code" because it is easy to read and understand.  I 
expect pretty much everyone in this group - even with no Python 
experience - to have a reasonable understanding of what it does.  You 
have to know a bit of Python in order to be able to /write/ the code, 
but not to understand it.

> 
> This is not really programming either. (You might as well write C code 
> consisting of sequences of system() calls. That might well invoke a 
> Python program.)

Of course it is programming!

> 
> And when I tried it, I got about 5 seconds on Py3, and 3.5 seconds on Pypy.
> 
> This is the C algorithm expressed in Python:
> 
>   def gethist(s):
>       counts=[0,]*256
>       for c in s:
>           counts[ord(c)]+=1
>       return counts
> 
> and is called like this:
> 
>   counts=gethist(msg)
>   for i in range(32,256):
>       if counts[i]:
>           print (chr(i),counts[i])
> 
> This version takes 12 seconds in Py3, but now allows pypy to do its 
> thing with loops; pypy takes only 0.6 seconds, comparing much more 
> closely with C.
> 

Now /that/ is an interesting result.  It is not the Pythonic way to 
write it, but I guess pypy can do a better job with such code.

For me, Python 3 gives 4.7 seconds here and pypy takes 0.3 seconds.

It is even closer to the C if the message is stored as a Python3 "bytes" 
object, rather than a string, and that pulls the Python 3 time down to 
3.2 seconds for me.  But pypy does not (currently) support "bytes".

For optimal Python code, the next step would be numpy (or numpypy for 
pypy).  But that is getting /way/ off-topic for here.

The point of my comments is that C is not necessarily faster than Python 
- Python code with a good algorithm and good implementation of the 
algorithm can be faster than C code with a weak algorithm, and because 
Python makes it much quicker and simpler to write some kinds of code, it 
can be a faster solution overall.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 12:48 -0400
  Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-20 18:08 +0100
    Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 09:32 -0400
      Re: Warning: assignment from incompatible pointer type scott@slp53.sl.home (Scott Lurndal) - 2017-06-21 13:48 +0000
  Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-20 18:15 +0100
    Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:00 -0400
      Re: Warning: assignment from incompatible pointer type "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 18:47 -0400
        Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-21 00:10 +0100
        Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 19:56 -0400
  Re: Warning: assignment from incompatible pointer type "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 13:23 -0400
    Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:59 -0400
  Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 19:23 +0100
    Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:59 -0400
      Re: Warning: assignment from incompatible pointer type Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-20 19:46 -0400
      Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-21 02:05 +0100
        Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 21:37 -0400
          Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-21 09:25 +0100
          Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-21 13:35 +0200
            Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 22:30 -0400
              Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 11:03 +0100
              Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 11:10 +0100
                Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 11:42 +0100
                Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 13:49 +0200
                Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 14:40 +0100
                Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 17:00 +0100
                Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 20:08 +0200
                Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-22 15:46 -0400
                Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 14:00 +0100
                Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 14:38 +0100
                Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 17:26 +0100
                Re: Warning: assignment from incompatible pointer type Keith Thompson <kst-u@mib.org> - 2017-06-22 10:21 -0700
                Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 18:26 +0100
                Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 20:41 +0200
                Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-22 19:59 +0100
                Re: Warning: assignment from incompatible pointer type Keith Thompson <kst-u@mib.org> - 2017-06-22 12:28 -0700
          Re: Warning: assignment from incompatible pointer type Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-21 09:49 -0400
            Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 10:36 -0400
              Re: Warning: assignment from incompatible pointer type Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-21 14:15 -0400
          Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-21 15:10 +0100

csiph-web