Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: number of unique characters in a string

by BrowserUk (Patriarch)
on Mar 01, 2003 at 04:49 UTC ( [id://239650]=note: print w/replies, xml ) Need Help??


in reply to number of unique characters in a string

Updated To correct error introduced whilst sanitising for public consumption.

It would be interesting to see how this would compare with merlyn's Inline::C version for performance. Unfortunately, I still don't have that ability (yet).

One point to note, this version should also work for unicode strings. If you don't need that, then swapping the unpack template from 'U*' to 'C*' should do the trick and might be a tad quicker.

#! perl -slw use strict; sub nUniqC{ my @uniq; ## Updated. @uniq[unpack 'U*',$_[0]] = (1)x length $_[0]; scalar (grep defined $_, @uniq); } print nUniqC '1010101010'; print nUniqC '1234567812';

For speed in perl and assuming no utf-8, this seems a bit quicker.

sub nUniqC{ my @uniq; scalar grep{ ++$uniq[$_] == 1 } unpack('C*',$_[0]); }

Update Trimmed another 15% of fat. No I didn't. I misread bad data, the version above is quickest.

sub nUniqC{ scalar grep{ ++$_[$_] == 1 } unpack('C*',$_[0]); }

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Re: number of unique characters in a string
by Limbic~Region (Chancellor) on Mar 01, 2003 at 04:59 UTC

      Limbic~Region++ for catching my stupidity.

      I updated. Try it now if you have the time. I know Merlyn's will scream relatively, it would be interesting to see just how much quicker it is? Not having had the chance to make this sort of comparison means I have no handle on the differences.


      ..and remember there are a lot of things monks are supposed to be but lazy is not one of them

      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found