That is a great point. I suppose I should clarify that statement by saying that my idea of "nearly exactly" in regards to translating to C was that it is much easier than say to forth - the math logic is the same and the functions are setup in a very similar manner.
In terms of length difference, I agree when dealing with strings, C ends up taking multiple lines to do what Perl can do in one line. Aside from that, the line count is IMO largely due to comments and I'm not really sure how many lines are just comments and haven't bothered to really determine that exactly. Were I to strip out all comment blocks from the C and Perl code - I would imagine the Perl to be shorter - I likely just estimated how many they have of each to be wrong.
Whereas if you can pare your code down so that the work gets done by map, foreach, tr, regexes etc then the execution time should drop.
I'm certainly interested in speeding it up just out of curiosity (since I already have a C version now that takes 1.4sec, which I don't foree the Perl version ever doing - but I am curious where I made decisions that slowed the Perl down).
I converted every for loop to a foreach loop instead - that slowed the code down by 6 seconds. I assume I didn't get the optimal use of it because I still need to be aware of where I am in the array and not just what $_ is, so I kept a counter along with it - which I would assume would be pretty much just like a for loop in that case.
Perhaps I'm just being dense, but the only place I see where map would really apply to my stuff is when I'm originally manipulating my arrays - and I've already benchmarked that to take less than a second, and it is only done once - so that isn't the speed concern there.
I'm only doing math inside the loops, so the tr is useless to me in that case - again, the only text manipulation I do is at the start and at the end of the program - outside of any of the loops. Other than that, it is just math.
Again, I don't see how a regex will help me when I just want to iterate N days of values and add them together, and then divide by some number.
Were I parsing text and seeing this huge a time difference, I would be very suspicious of it all - but this is nearly all math inside the loops (3000+ times it iterates through the outer loop, and inside of that is a large 1000+ loop iteration with a few smaller 5-20 iterations that depenind on what algorithm is used is done on each 1000 as well).
I understand that I'm being pretty useless in not putting up the actual code so that people can point out "you did this and THIS would be way faster" - but I think in the end I mainly wanted to show another example with some numbers of what speed differences for various applications exist. When I was orginally going to port it over to C, I looked around for numbers of what sort of difference I might see, and very few people talk about doing it.
So now there is at least one more node about it :)
Is there a resource out there that lets me know that "doing XYZ is more efficient in Perl than doing ABC"? I'm curious which things I'm doing that might be sped up, but the bulk of what is inside the loops is if/else statements, and just basic math ((a+b)/c)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.