Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Anyway to Have Strong-Like Typing

by jmmitc06 (Beadle)
on Aug 11, 2014 at 20:12 UTC ( [id://1097030]=perlquestion: print w/replies, xml ) Need Help??

jmmitc06 has asked for the wisdom of the Perl Monks concerning the following question:

Monks:

I have a large chunk of code where I am doing some heavy mathematical work and I would like to know if there are any options to define what type a variable is in order to avoid alot of the interpreter work with the loose/dynamic variable typing. I know that this is unlikely given it is a very non-Perl way of doing something but in short I would like to tell the interpreter that variable $foo is and will always be an integer for example, so don't bother checking if it is anything else. From my understanding, many operations that may be performed on $foo will check the type of the variable before proceeding, deciding how to do the operation.

Additionally, I know that rewriting in another language (C, C++) where this is available by default would provide these advantages, but this is not a good option given that the rest of the program is in Perl and I'm not a programmer by trade, so a C/C++ rewrite will take much longer than implementing the other solution if available. Additionally, it likely won't be a huge benefit in performance but now I'm more interested out of curiosity than anything else.

Thanks in advance for any thoughts on this. As a biochemist without a formal programming background, the PerlMonks site has been a great resource to me in the last few years both from answering the questions I have asked and the volume of stuff available from the questions of others

Replies are listed 'Best First'.
Re: Anyway to Have Strong-Like Typing
by kennethk (Abbot) on Aug 11, 2014 at 20:35 UTC
    Perl is actually a very strongly typed language; the issue is that those types are scalar, array, and hash (plus some other fun ones that generally stay in the background). Scalars are essentially all strings with a bunch of magic around numerical data content (again, simplification). I think we've got a bit of an XY Problem - I think what you really want to know is how to heavily optimize some mathematical operations.

    First, profile your code. Make sure the slow chunks really are where you think they are. I like Devel::NYTProf, particularly with its nytprofhtml utility. It makes drilling though profiles fast and easy.

    Second, assuming the math is the slow part, I can think of two fairly easy solutions:

    1. PDL. From the docs:

      PDL is the Perl Data Language, a perl extension that is designed for scientific and bulk numeric data processing and display.
      I haven't used it, but depending on the math you want to do, it's probably the lowest overhead dive to get good performance.
    2. Inline::C. In this case, I have used it, and I like it a lot. However, it will require that you program in C in order to do your fast operations. It buys you ease in terms of getting the Perl code to call C.

    You can't get something for nothing, but if you are doing heavy numerics in Perl, there is almost assuredly opportunity for growth.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks for the responses, I have seen Math::Integer before and have played with it.

      I have heavily profiled the code with NYTProf making improvements here and there for the last few months with a major rewrite the other day. The slow part is the math, mainly one nasty do { } while ( ) construct. I think that I've reached the limit of what I can optimize, this implementation is already a heavily optimized / rewrite of another algorithm, and my question was a long shot and curiosity. Normally, I wouldn't really bother optimizing it since it is "fast enough", but it's been a slow week at work lol. Yeah, I'll probably try an Inline::C implementation before a true C rewrite when the free time comes to do it.

      I do think it would be interesting if we could have access to strongly typing all the variable types in C when we need it, and the more easy to work with standard Perl types the rest of the time.

        The slow part is the math, mainly one nasty do { } while ( ) construct.

        Post it. It often helps to have fresh eyes look at these things, and there are people around here that have unique ways of looking at code.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        I fully agree with BrowserUK's thought: for numeric processing, PDL is highly likely to be the best solution - both for actual numerical performance (in many cases it can automatically use multiple CPU cores "for free"), and for speed of development (the REPL, "perldl" or "pdl2", are genuinely quite fun to use).
Re: Anyway to Have Strong-Like Typing
by Anonymous Monk on Aug 11, 2014 at 20:55 UTC

    This is likely not what you want, but since you mentioned integers, I just wanted to point out use integer; in case you haven't seen that yet.

    Other than that, I'd second kennethk's post, all very good suggestions.

      I'll third the AnonyMonk's second, and only add that the integer pragma is lexically scoped, so there's some leeway for creative use of it:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $z = S(2, 7); print $z; $z += 2.345; print $z; ;; sub S { use integer; my ($x, $y) = @_; return $x * ($y + 1.234); } " 16 18.345

      Oh, and BTW, integer applies to operations, not variables!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097030]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found