Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
7 total  XML / RSS feed 

Sizeof() vs. Count()

// from http://digest.phpit.net/archives/23/html/. They didn't cite a source, so I'm not so sure about this.

Quick Tip: use sizeof() instead of count()

If you’re writing a PHP5 script, use the sizeof() function instead of count(). Although the manual says that sizeof() is a simple alias for count(), sizeof() is actually twice as fast, and can give a slight increase in performance, especially when used a lot.

In one of my scripts I saw performance increase by a whole second by using sizeof() instead of count(). I’m not sure why this is, especially since it’s officially an alias, but that’s just the way it is. Use it to your advantage!

How to count particular words using perl

When "grep -c" doesn't cut it (counts only 1 occurrence per line).

perl -lne '$c++ while /SEARCH_STRING/g; END { print $c; }'


For example, to count the number of times ABCD appears in mysqldump output:

mysqldump my_db | perl -lne '$c++ while /ABCD/g; END { print $c; }'


Modified version of snippet found in http://www.wellho.net/forum/Perl-Programming/Issue-with-Grep.html

How to count particular words in vim

From one of the comments in http://www.vim.org/tips/tip.php?tip_id=689

:%s/word/&/g


The above substitutes "word" for itself, and tells you the number of substitutions made.

count duplicate users and store in db - sql

// count duplicate users and store in db

// count users with filter
select Count(surname+firstname) as duplicates, surname, firstname
from c_contact
where show_record=1
group by surname, firstname
having Count(surname+firstname) > 1
order by duplicates, surname, firstname

// original code
SELECT column,COUNT(*) c 
FROM table
GROUP BY column 
HAVING COUNT(*) > 1

//update code
update c_contact
set duplicates = dbo.func_get_duplicates(surname, firstname)

count characters

// description of your code here

class Counter
  
def initialize()
  @characters = Hash.new(0)
end

def read()
  @text = IO.read("text.txt")
end

def count_chars
  @text.each_byte do |ch|
  @characters[ch] +=1
  end
end

def report  
  @characters.each do |key, value|
    puts "#{key.chr} (#{key}) occurs #{value} times"
  end  
end

end

Actionscript

// Countdown code

countDown = function() {
        seconds_conta = seconds_conta - 1;
        hours = int(seconds_conta / 3600);
        minutes = int((seconds_conta / 60) - (hours * 60));
        //seconds = int(seconds_conta-((hours*3600)+minutes*60));
        seconds = int((seconds_conta % 60));
        
        //Se for menor que dez, acrescenta um zero
        if(minutes<10){
                minutes="0"+minutes;
        }
        if(seconds<10){
                seconds="0"+seconds;
        }
        
        //Será que precisa colocar no frame correto?
        timer_txt.text=(hours+":"+minutes+":"+seconds);
        
                //trace("seconds_conta: "+seconds_conta);
                //trace("hours: "+hours);
                //trace("minutes: "+minutes);
                //trace("seconds: "+seconds);
        
        if (seconds_conta == 0) {
          clearInterval(timer);
                  //Chama função de acabar a contagem!
                  //fim_das_questões();
     }
} 

///////// Acrescentar no código
//Tempo total inicial em segundos ->Calculado a partir da quantidade de questões
seconds_conta = 185;
///////// Inicia o timer
//A cada 1000 ms, ativa a função countDown, diminuindo um segundo do total
timer = setInterval(countDown, 1000);

C macro that implements count_if, sum, max_elem

// C macro that does count_if, sum, max_elem.

#define COUNT_IF(ITERATION, CONDITION, COUNT)  {COUNT =0; ITERATION {if(CONDITION) COUNT++;}}  
/* ex: COUNT_IF( for(i=0;i++;i<10), i%2==0, evens)  [counting the number of evens.]  */

#define SUM(ITERATION, EXPRESSION, SUMM)  {SUMM =0; ITERATION {SUMM +=(EXPRESSION);}}  
/* if ITERATION is an empty iteration, the sum is 0
SUM( for(i=0;i<60;i++) if(zik[i]==sol[i]) , score[i], actual_score ) */


#define MAX_ELEM(ITERATION, EXPRESSION, MAX_VALUE, POSITION)  { \
        bool first233496 = true;                                   \
        ITERATION {                                               \
                if(first233496){ first233496 = false; POSITION ; MAX_VALUE =(EXPRESSION); } \
                else if((EXPRESSION)> MAX_VALUE ){ POSITION ; MAX_VALUE =(EXPRESSION); }   \
        }                                               \
}
/* if ITERATION is an empty iteration, this macro does not work.
ex: MAX_ELEM(for(x=0;x<5;x++) for(y=0;y<5;y++) , x-2*y , maximum , {m_x=x;m_y=y;} );  [ Set (m_x,m_y,maximum) := (x,y,x-2*y) when x-2*y achieves maximum value] 
ex: MAX_ELEM( ON(i,10), (10-v[i])*v[i], d, k=i); */
« Newer Snippets
Older Snippets »
7 total  XML / RSS feed