Re: does a hash element as a loop parameter cause significant slowdown?
by afoken (Chancellor) on Sep 08, 2023 at 18:58 UTC
|
ChatGPT suggested ...
ChatGPT is optimized to give answers that look plausibe at first glance. It does not mean that they are. Most likely, there are not. You better treat ChatGPT and other AI systems as extremely stupid, yet gifted scammers.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
Re: does a hash element as a loop parameter cause significant slowdown?
by kcott (Archbishop) on Sep 09, 2023 at 07:09 UTC
|
G'day misterperl,
Use Benchmark to check for "significant slowdown".
More generally, "perlperf - Perl Performance and Optimization Techniques"
provides useful information.
Also be aware that Perl is being continually optimised:
some code might be slower than other code for a particular Perl version;
in a subsequent version there may be no difference.
If you post benchmark code and results, we'll be happy to discuss.
You may also receive alternative code that you hadn't considered and may be better for your specific needs.
| [reply] |
|
> Use Benchmark to check for "significant slowdown"
Yes. I really like TheDamian's simple adage "Don't Optimize Code -- Benchmark It" -
if you don't follow his advice, you'll probably end up with a fast slow program. :)
Without good design, good algorithms, and complete understanding of the
program's operation, your carefully optimized code will amount to one of
mankind's least fruitful creations -- a fast slow program.
-- Michael Abrash
Correctness, simplicity and clarity come first ... so you shouldn't really
consider optimizing that little piece of code until you've benchmarked the entire program
and identified it as a chronic bottleneck.
For an example of using Benchmark to compare different snippets of Perl code see: Re^3: looping efficiency (Benchmark Example)
Updated: added Michael Abrash quote to clarify what I meant by a fast slow program.
Added Correctness, simplicity and clarity paragraph.
| [reply] |
Re: does a hash element as a loop parameter cause significant slowdown?
by tobyink (Canon) on Sep 09, 2023 at 13:16 UTC
|
for my $c (1..$data->{n1}) {
Will not be faster by allocating to an intermediate $n1 variable. It may even be a couple of nanoseconds slower.
The second part:
$data->{"exclude-$c-$_"} = 'yes' for 1..$data->{n2};
May be very slightly faster if you assign an intermediate $n2 variable before the outer loop because it prevents a hash lookup for each iteration of the outer loop. However with your n1 being "like 10 or less", this is not likely to make any practical difference in speed. If you were dealing with millions of iterations of the outer loop, it might be worth optimizing.
| [reply] [d/l] [select] |
Re: does a hash element as a loop parameter cause significant slowdown?
by jwkrahn (Abbot) on Sep 08, 2023 at 19:01 UTC
|
for my $c (1..$n1) {
The list 1..$n1 is created in memory and then iterated over so $n1 is only evaluated once.
If you instead used a C style for loop then $n1 would be evaluated $n1 times.
Naked blocks are fun!
-- Randal L. Schwartz, Perl hacker
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
Maybe 5.10ish?
I checked perldoc.perl.org's 5.10 docs: it still had the "but older versions" comment.
So I checked 5.005's docs: it still had the "but older versions" comment.
So if perldoc.perl.org's copies of the docs are accurate, it was documenting the list-in-memory behavior as "old" even at 5.005!
/me guesses he needs to get over his aversion to large ranges in foreach loops ;-)
| [reply] |
|
Re: does a hash element as a loop parameter cause significant slowdown?
by Bod (Parson) on Sep 08, 2023 at 22:48 UTC
|
ChatGPT suggested...
I have noticed that some monks seem quite against ChatGPT and its ability to provide useful information. I am not one of those monks. I make a lot of use of AI tools, not just ChatGPT.
However, you have to be aware that ChatGPT is like a little puppy. It is always out to please you and make you happy. To this end, it will give you answers that it wants you to hear. Even if they are totally wrong. This can be improved a bit by better prompting, especially letting the AI know that it can ask questions of you if needed.
ChatGPT is, in my experience, not the best tool for providing code although it can help you understand what is going on in a piece of code that you are struggling with. I would suggest you look at Perplexity for starters. Unlike ChatGPT, it also searches the web to find the answers to your questions and provides the sources it has found so you can check them out for credibility. I find that Perplexity often uses PerlMonks as a source for Perl information so, if nothing else, it knows to look in the best place 😁
| [reply] |
|
Many thanks all MONKS, especially Rolf, ++ votes for your intuition and guidance. Like you say , I've found sometimes CHATGPT returns startlingly "wrong" and misleading results.
THEDAMIAN?? haaa! I've met DAMIAN at many OSCON's and watched his amazing videos. He has one on vi that still gives me nightmares :) An amazing guy for sure I like to listen to him with the hope that I can use and understand 1% of what he knows!
Cheers
| [reply] |
Re: does a hash element as a loop parameter cause significant slowdown?
by Anonymous Monk on Sep 08, 2023 at 18:45 UTC
|
Please ask ChatGPT to define "significant slowdown". | [reply] |