Gist of the Day: Ends in Four

Sometimes we have a simple task to do and we over-think it. Such was the case when someone asked me an interview question one time. They said, write me a quick function to prove that a provided integer ends in the number four. Instantly, without really thinking about it, bit operations popped into my head. Continue reading Gist of the Day: Ends in Four

Gist of the Day: Analyzing Performance with Benchmark.pm

In my post on Inline::C a few days ago I mentioned The Rules of Optimization Club, and then I ranted a little bit about how if you cannot measure a performance problem then you don’t have a performance problem. That’s not to say that you’re incorrect in asserting that you have a performance problem, it is only to say that you cannot identify any particular part of the problem as a performance problem until you have measured it.
There a great number of ways to measure performance problems, profiling probably being the most useful in situations involving large applications where you want to test performance under real-life situations. For profiling, I would recommend you look at Devel::NYTProf. This profiler is exceptionally feature-rich and has a boatload of useful functionality. All that said, here I’m only going to talk about bench-marking small pieces of code. Continue reading Gist of the Day: Analyzing Performance with Benchmark.pm

Gist of the Day: Some convenience macros for use with Perl C internals

I feel awful. Like, really awful. The baby is sick, my partner is sick, I am sick. You still want something neat though, so I’m going to show you something very simple that I’ve used for a while.
As I’m sure you noticed in yesterday’s Inline::C demo, Perl guts and C API are pretty noisy with a lot of boiler-plate. For this reason, when I first started playing with Perl guts and API, I created a header file just to make things a little simpler. Nothing here is terribly complicated, and they’re all just simple convenience macros, but sometimes that type of macro saves you oodles of time. Continue reading Gist of the Day: Some convenience macros for use with Perl C internals

Gist of the Day: Inline::C in Perl

I like Perl and I like C (most of the time), and sometimes I like to mix the two. The two main reasons I might want to mix the two is for performance, or because something is written in C which I would like to use from Perl. I’ve only really ever used C from Perl, I’ve never used Perl from C. Today’s demonstration is how to implement a simple binary search algorithm in C, but using Perl internals, and calling the algorithm from Perl. Continue reading Gist of the Day: Inline::C in Perl

Gist of the Day: Simple Threading in Perl

So today’s gist is a simple example of how to do two different things with two separate threads. Threading in Perl is misunderstood and often belittled, but it’s pretty straight-forward. The #1 problem that I think people encounter when threading in Perl is that they are using non-thread-safe code. If you’re using a CPAN module, you need to make sure you evaluate it for thread-safety prior to running it with threads (and if it’s using XS, it likely isn’t).
Anyway, this gist takes a function which crunches a set using a calculation routine. The calculation routine takes three parameters:

  1. The value so far
  2. N
  3. And the next value in the set

From there, the calculation routine performs the appropriate action on the next value in the set – given the prior value and the value of N – and then returns the result.
Here’s the Gist: https://gist.github.com/manchicken/6349415