Thread: C or python or perl?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    C or python or perl?

    I've been trying to write a math engine in C. Well, i've had a really really really hard time parsing all the inputs and stuff with see with the continious shower of seg faults. I want to switch to either python or perl since both do input stuff really well but, python is slower (based on a few benchmarks found on the 'net) and perl is okay whereas C is blazing fast. Will i be laughed at if I use one the interpreted langs?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    use whatever language you want.

    you could also try posting your code here for review.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Poincare View Post
    Will i be laughed at if I use one the interpreted langs?
    Maybe by your grandparents and a few stragglers living in caves, etc.

    One thing that I have done before and that I believe many other people do, altho I don't know the term for it, is work some functionality (such as an algorithm) out in perl, since the amount of coding is orders of magnitude less, and then implement it in C -- so I focus on high level concerns, and then the lower level nitty gritty optimization. Both perl and python are written in C (I think python leans on C++ more for extensions, maybe that is why perl is faster ), and I will say that at least with perl it is surprising the extent to which you can "see the relationship" even when the syntax is quite different, altho perl does often explicitly and directly rely on the same semantics as the underlying C functions. I haven't used python for much except "gimp-fu", so I don't know as much about it.

    I use both C and perl on a daily basis and cannot claim to have done anything in one language that I cannot do in the other, and I don't feel I'm "better" in one or the other either, this is just the reality of what they are I suppose.

    If your problem is seg faults, which are usually about memory management, and error reporting, which is generally better with interpreted languages, then go for it. While perl has much larger available set of libraries/modules, like second to nothing, and maybe better on-line resources, I would say python is less idiosyncratic, much easier to learn, more normative OO and with a very rapidly growing user base, so it might be better for you. They will both do want you want, anyway; perl has been used to do high performance 3D graphics and is widely used in scientific research, such as bioinformatics; I imagine the same is true of python.

    Scripting languages rock, don't let anyone tell you otherwise.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can try C++ too. It's fast (as fast as C) and has functionality / ease to match python/perl.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Elysia View Post
    You can try C++ too ... ease to match python/perl.
    Not in everything. Good luck with quick, dirty string processing

    > I've been trying to write a math engine in C. Well, i've had a really really really hard time parsing all the inputs and stuff
    How are you parsing things? You could always use a lexer like flex.
    Last edited by zacs7; 09-07-2009 at 04:39 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    has functionality / ease to match python/perl.
    Good luck with everything from the first character on. That is way way WAY out to lunch...do you know what a sigil is? Etc. These things are worlds apart by design and necessity. The more complex the demo you propose, the further apart the byte counts for the source will become. I promise. At around 2-300 lines, with the perl version less than 50, this difference starts to seem extremely significant.

    Vis. math parsing, I wrote this a while ago for something (in C), you can take a look; the first two do roman numerals, the third one does normal expressions like (2+2)*7, with commentary:

    http://www.intergate.com/~halfcountp...P/parsing.html
    Last edited by MK27; 09-07-2009 at 04:54 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by zacs7 View Post
    Not in everything. Good luck with quick, dirty string processing
    Patently untrue. I've written many parsers and other various string-processing routines in C++ without any headaches whatsoever. Not only can it be just as easy as with an interpreted language, it can be much more so (thanks to operator-overloading, among other things), and also much more powerful and flexible (thanks to templates, your code works with arbitrary types (char, float, a custom type, etc), and parses not just "flat" char* strings, but any kind of object that can be iterated (lists, vectors, files)). Try getting *that* to work with perl or python!

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Python. Python is my dirty lover, and it's been good to me.

    It's possible switching to C++ might help you in this case, if it helps you with memory management. I believe the majority of the coders using languages purely for math algorithms use C though, and Python for quick testing. I remember a lot of complaints about Python's core global interpreter lock. So computing an algorithm in parallel is blocked (threads are useless) - think there's a workaround.

    Quote Originally Posted by Sebastiani View Post
    Patently untrue. I've written many parsers and other various string-processing routines in C++ without any headaches whatsoever. Not only can it be just as easy as with an interpreted language, it can be much more so (thanks to operator-overloading, among other things), and also much more powerful and flexible (thanks to templates, your code works with arbitrary types (char, float, a custom type, etc), and parses not just "flat" char* strings, but any kind of object that can be iterated (lists, vectors, files)). Try getting *that* to work with perl or python!
    I dunno bro, Python's string parsing is so much more convenient and readable. In the long-run it doesn't matter, but scripting languages are masters of the short-run.

    someString[5:10]
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Dae View Post
    Python. Python is my dirty lover, and it's been good to me.

    It's possible switching to C++ might help you in this case, if it helps you with memory management. I believe the majority of the coders using languages purely for math algorithms use C though, and Python for quick testing. I remember a lot of complaints about Python's core global interpreter lock. So computing an algorithm in parallel is blocked (threads are useless) - think there's a workaround.



    I dunno bro, Python's string parsing is so much more convenient and readable. In the long-run it doesn't matter, but scripting languages are masters of the short-run.

    someString[5:10]
    Hmm, let's see:

    Code:
    /*
        Read a phone number in the format (XXX) XXX-XXXX
    */
    template < typename Iterator, typename Result >
    bool read_phone_number
    ( 
    	Iterator begin, 
    	Iterator end, 
    	Result& area_code, 
    	Result& exchange, 
    	Result& extension 
    )
    {
    	using namespace 
    		xtd::parser;
    	static syntax< >
    		ws = skip( token_range( " \n\t" ) ),
    		lp = ws + '(' + ws, 
    		rp = ws + ')' + ws, 
    		dash = ws + '-' + ws,
    		digit = terminal_range( '0', '9' ),	
    		digits_3 = ws + ( digit * 3 ) + ws, 
    		digits_4 = ws + ( digit * 4 ) + ws;
    	return
    	(
    		lp + ( digits_3 >> store( area_code ) ) + rp 
    		+ ( digits_3 >> store( exchange ) ) + dash 
    		+ ( digits_4 >> store( extension ) )
    	).parse_all( begin, end );
    }
    
    template < typename Container, typename Result >
    inline bool read_phone_number
    ( 
        Container const& data, 
        Result& area_code, 
        Result& exchange, 
        Result& extension 
    )
    {
        return read_phone_number( data.begin( ), data.end( ), area_code, exchange, extension );
    }
    That's a very simple example, of course, but extending it to accept more formats would be trivial. And note that the parser doesn't skip whitespace by default, but that could be easily built into a library, if desired.

    Here's a sample test program:

    Code:
    int main( void )
    {
        string
            input_as_string = "( 800 )555 - 1212";
        list< int >
            input_as_list_of_ints( input_as_string.begin( ), input_as_string.end( ) );
        vector< double >
            numbers_as_doubles( 3 );
        vector< int >
            numbers_as_ints( 3 );
        assert
        ( 
            read_phone_number
            ( 
                input_as_string, 
                numbers_as_doubles[ 0 ], 
                numbers_as_doubles[ 1 ], 
                numbers_as_doubles[ 2 ] 
            )
        );    
        assert
        ( 
            read_phone_number
            ( 
                input_as_list_of_ints, 
                numbers_as_ints[ 0 ], 
                numbers_as_ints[ 1 ], 
                numbers_as_ints[ 2 ] 
            )
        );            
        copy
        ( 
            numbers_as_doubles.begin( ), 
            numbers_as_doubles.end( ), 
            ostream_iterator< double >( cout, "\n" ) 
        );
        copy
        ( 
            numbers_as_ints.begin( ), 
            numbers_as_ints.end( ), 
            ostream_iterator< int >( cout, "\n" ) 
        );
        return 0;
    }
    Now it may seem silly to pass the input as a list< int > or place the result into a double, but my point is that it is incredibly flexible, and AFAIK would quite complicated to work out with a scripting language (correct me if I'm wrong though).

    Besides all of this, the parsing is done roughly as fast as had you done it "by hand" (maybe 5% slower, max).

    Anyway, so my whole point is that C++ is more than adequate for this sort of thing (although you may need to do a fair bit of coding, initially). Not that I think there's anything wrong with using a scripting language, either - I'm just defending C++ position as a contender.

    EDIT:
    OK, well the assertion failed. Wrong grammer. Fixed.
    Last edited by Sebastiani; 09-07-2009 at 07:13 PM.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    That code is pretty sick, Sebastiani. I don't know how that relates to what was said about Python at all. Python usage code... there's no comparison. They are completely different, and the comparison was made that C++ can "easily" match scripting languages. Iterators, lambdas, tuples, templates (sort of), etc. are built right into the syntax.

    That's not really what I meant, and if you don't understand I wonder how much experience you have with Python, Perl, Ruby, JavaScript, ActionScript, even PHP, etc. There's no compiling. There's no linking. Most libraries are pre-installed. You don't even need a file. That is "quick, dirty string processing" at it's best.

    Regardless, Google'd Python version..

    Code:
    import re
    phoneNumber = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$', re.VERBOSE)
    >>> phoneNumber.search('800-555-1212')
    ('800', '555', '1212', '')

    >>> phoneNumber.search('work 1-(800) 555.1212 #1234')
    ('800', '555', '1212', '1234')

    >>> phoneNumber.search('( 800 )555 - 1212')
    ('800', '555', '1212')
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I was simply responding to the claims that C++ was somehow not as useful as scripting languages at parsing/string-processing, which is obviously *completely ludicrous*. Just because you can write a bit of code "without compiling, without linking, without having to save it in a file" means nothing. What did you save - 2 minutes?

    What I am talking about is solving a problem. Does it take an *appreciable* amount of time to get something done with technology X? If so, then use something else. Is this the case with C++, vs. some scripting language? Emphatically, the answer is: NO! So please, come down from your high horse (or python, as it were), because your "scripting languages are better" argument just doesn't hold water.

    Also, the snippet you posted is indeed brief, but apparently too cryptic to produce without the aid of Google. How ironic.

  12. #12
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Sebastiani View Post
    I was simply responding to the claims that C++ was somehow not as useful as scripting languages at parsing/string-processing, which is obviously *completely ludicrous*. Just because you can write a bit of code "without compiling, without linking, without having to save it in a file" means nothing. What did you save - 2 minutes?
    2 minutes? Scripting languages save you a LOT more time than that -- granted you learn less. I think you're misunderstanding us for some sort of scripting language fanatics, because I love C++, and it can do everything Python can, and be just as useful, but it's not dirty.. not very often.

    Quote Originally Posted by Sebastiani View Post
    What I am talking about is solving a problem. Does it take an *appreciable* amount of time to get something done with technology X? If so, then use something else. Is this the case with C++, vs. some scripting language? Emphatically, the answer is: NO! So please, come down from your high horse (or python, as it were), because your "scripting languages are better" argument just doesn't hold water.
    Nobody said scripting languages were better. Don't misquote me. Sounds like you're the one on a high horse (the C++ horse). Use the right tool for the job. It just so happens that scripting languages (perl/python) are better at quick and dirty stuff.

    Quote Originally Posted by Sebastiani View Post
    Also, the snippet you posted is indeed brief, but apparently too cryptic to produce without the aid of Google. How ironic.
    No.. I know regex, but I'm not going to be bothered to work on it myself for some elaborate response when your argument made no sense to begin with. The really funny thing is boost::regex is quite brief as well, not as much, but still (also a little annoying with the current state of string literals - until c++0x I believe).
    Last edited by Dae; 09-07-2009 at 10:09 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Nobody said scripting languages were better. Don't misquote me.
    I wasn't quoting you, actually. But if I may:

    It just so happens that scripting languages (perl/python) are better at quick and dirty stuff.
    So which is it?

    Anyway, my main argument was simply that C++ is equally as useful (and indeed "the right tool for the job"). How exactly does that make "no sense to begin with"? As I said, I was merely defending the language, really. I don't mind people promoting scripting languages - they are, obviously, also quite useful (I use them almost every day) - but when I hear claims to the effect that they leave languages such as C++ "in the dust"...that's just nonsense, pure and simple.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the two sentences you quoted aren't incompatible, Sebastiani, so why make people choose?

  15. #15
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Sebastiani View Post
    I wasn't quoting you, actually. But if I may:



    So which is it?

    Anyway, my main argument was simply that C++ is equally as useful (and indeed "the right tool for the job"). How exactly does that make "no sense to begin with"? As I said, I was merely defending the language, really. I don't mind people promoting scripting languages - they are, obviously, also quite useful (I use them almost every day) - but when I hear claims to the effect that they leave languages such as C++ "in the dust"...that's just nonsense, pure and simple.
    The reason it makes no sense, Sebastiani, is because C/C++ maybe be quick and easy for you, but the topic is the OP (original poster) not you. He's just learning the language, and keeps running into seg faults. Do you honestly thing he won't save a serious amount of time using a scripting language, versus learning the intricacies of C/C++? You know C++ so well your syntax emulates the C++ standard library (like a lot of people here). That code you wrote up may be easy for you but the OP would have to learn a lot more subjects and syntax to use it, let alone create it himself (it takes a certain experience, without googling everything - and I mean everything - and possibly asking questions), compared to a scripting language. I've never even used xtd. Even I'd have to learn that library. On the other hand I've used the exact same perl-style regular expressions in Perl, PHP, JavaScript, Python, and C++ (but ran into difficulties with string literals and learning the boost::regex library).

    It's almost like your recommendation is learn C++ for 5 years, then you will be able to produce code using C++ as efficiently as you would in Python. I will say if someone knew C++ as well as you, they shouldn't use Python (unless maybe web based). Fact. I know C++ less than you do, and I still won't use Python for most projects. I'm willing to put in the time to learn it. I recommend learning both though, as they both teach you a lot about computer science (C++ more-so, and Python quicker - ie Java argument - Java 2x faster than C++, Python 2x faster than C++ - for newbies).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. python c api
    By ralu. in forum C Programming
    Replies: 0
    Last Post: 03-01-2009, 01:19 PM
  2. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 10:19 AM
  3. Python
    By Xterria in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-05-2002, 04:08 PM
  4. perl need help pls.....
    By magnum38 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-12-2001, 10:35 PM