Hello Evrybody

please can anyone help or explain to me how can i find time taken for a code to execute.I have put clock in main and its working.But now i have to find time outside main.The clock statement is not working
i have added
clock_t start = clock(); and printf("first Time Elapsed : %f\n", ((double)clock() - start) / CLOCKS_PER_SEC) between code but in main it is working but outside it not.

what should i do.In fact iwant to calculate time for different module.

hope someone will help me out

thanks

Recommended Answers

All 6 Replies

"It doesn't work" is hardly a useful problem report. Why don't you post the code that "doesn't work" and tell us what you expected it to do. Then we'll tell you what you did wrong.

Also try reading the useful information in this thread

For trivial programs, using clock() is like using a sun-dial to measure the speed of a Ferrari - "Why is it always 0, because it's too damn fast that's why!".

Even the most optimistic precision (not accuracy) figures for clock() are at least 1M times slower than the clock frequency of most modern desktop machines.

Hello Narue,

sorry i dont know ur name.i am sending you the code again.i want to find the time taken for each phase in the code: time taken for phase 1 step 1 to execute,phase1 step2..etc.when i add the clock statement in the main..it works..but it doesnt when i place it between th phases or from phase1 up to last phase.

hope u do help me.

thanks

void mix_key(   unsigned char   *key,
  unsigned char   *ta,
  unsigned long int pnl,  /* Least significant 16 bits of PN */
  unsigned long int pnh,  /* Most significant 32 bits of PN */
  unsigned char   *rc4key,
  unsigned int *p1kout
     );
/************************************************************/
/* tkip_sbox()                                              */
/* Returns a 16 bit value from a 64K entry table. The Table */
/* is synthesized from two 256 entry byte wide tables.      */
/************************************************************/
unsigned int tkip_sbox(unsigned int index)
{
   unsigned int index_low;
    unsigned int index_high;
    unsigned int left, right;
    index_low = (index % 256);
    index_high = ((index >> 8) % 256);
    left = Tkip_Sbox_Lower[index_low] + (Tkip_Sbox_Upper[index_low] * 256);
    right = Tkip_Sbox_Upper[index_high] + (Tkip_Sbox_Lower[index_high] * 256);
    return (left ^ right);
};
/****************************************************/
/* mix_key()                                        */
/* Takes a key, PN and TK. Calculates an RC4 key.   */
/****************************************************/
unsigned int rotr1(unsigned int a)
{
    unsigned int b;
    if ((a & 0x01) == 0x01)
    {
 b = (a >> 1) | 0x8000;
    }
    else
    {
 b = (a >> 1) & 0x7fff;
    }
    b = b % 65536;
    return b;
}
void mix_key(   unsigned char   *key,
  unsigned char   *ta,
  unsigned long int pnl,  /* Least significant 16 bits of PN */
  unsigned long int pnh,  /* Most significant 32 bits of PN */
  unsigned char   *rc4key,
  unsigned int *p1k
     )
{
    clock_t start = clock();
    //clock_t start1 = clock();
    /* 16 bit numbers */

    unsigned int tsc0;
    unsigned int tsc1;
    unsigned int tsc2;
    unsigned int ppk0;
    unsigned int ppk1;
    unsigned int ppk2;
    unsigned int ppk3;
    unsigned int ppk4;
    unsigned int ppk5;
    int i;
    int j;
 
    tsc0 = (unsigned int)((pnh >> 16) % 65536);  /* tsc0 is most significant */
    tsc1 = (unsigned int)(pnh % 65536);
    tsc2 = (unsigned int)(pnl % 65536);          /* tsc2 is least significant */
    /* Phase 1, step 1 */
    p1k[0] = tsc1;
    p1k[1] = tsc0;
    p1k[2] = (unsigned int)(ta[0] + (ta[1]*256));
    p1k[3] = (unsigned int)(ta[2] + (ta[3]*256));
    p1k[4] = (unsigned int)(ta[4] + (ta[5]*256));
 
    /* Phase 1, step 2 */
    for (i=0; i<8; i++)
    {
 j = 2*(i & 1);
 p1k[0] =  (p1k[0] + tkip_sbox( (p1k[4] ^ ((256*key[1+j]) + key[j])) % 65536 )) % 65536;
 p1k[1] =  (p1k[1] + tkip_sbox( (p1k[0] ^ ((256*key[5+j]) + key[4+j])) % 65536 )) % 65536;
 p1k[2] =  (p1k[2] + tkip_sbox( (p1k[1] ^ ((256*key[9+j]) + key[8+j])) % 65536 )) % 65536;
 p1k[3] =  (p1k[3] + tkip_sbox( (p1k[2] ^ ((256*key[13+j]) + key[12+j])) % 65536 )) % 65536;
 p1k[4] =  (p1k[4] + tkip_sbox( (p1k[3] ^ (((256*key[1+j]) + key[j]))) % 65536 )) % 65536;
 p1k[4] = (p1k[4] + i) % 65536;

    }

    /* Phase 2, Step 1 */
    ppk0 = p1k[0];
    ppk1 = p1k[1];
    ppk2 = p1k[2];
    ppk3 = p1k[3];
    ppk4 = p1k[4];
    ppk5 = (p1k[4] + tsc2) % 65536;
    /* Phase2, Step 2 */
    ppk0 = ppk0 + tkip_sbox( (ppk5 ^ ((256*key[1]) + key[0])) % 65536);
    ppk1 = ppk1 + tkip_sbox( (ppk0 ^ ((256*key[3]) + key[2])) % 65536);
    ppk2 = ppk2 + tkip_sbox( (ppk1 ^ ((256*key[5]) + key[4])) % 65536);
    ppk3 = ppk3 + tkip_sbox( (ppk2 ^ ((256*key[7]) + key[6])) % 65536);
    ppk4 = ppk4 + tkip_sbox( (ppk3 ^ ((256*key[9]) + key[8])) % 65536);
    ppk5 = ppk5 + tkip_sbox( (ppk4 ^ ((256*key[11]) + key[10])) % 65536);
    ppk0 = ppk0 + rotr1(ppk5 ^ ((256*key[13]) + key[12]));
    ppk1 = ppk1 + rotr1(ppk0 ^ ((256*key[15]) + key[14]));
    ppk2 = ppk2 + rotr1(ppk1);
    ppk3 = ppk3 + rotr1(ppk2);
    ppk4 = ppk4 + rotr1(ppk3);
    ppk5 = ppk5 + rotr1(ppk4);
    /* Phase 2, Step 3 */
    rc4key[0] = tsc2 % 256;
    rc4key[1] = (((tsc2 / 256) % 256) | 0x20) & 0x7f;
    rc4key[2] = (tsc2 / 256) % 256;
    rc4key[3] = ((ppk5 ^ ((256*key[1]) + key[0])) >> 1) % 256;
    rc4key[4] = ppk0 % 256;
    rc4key[5] = (ppk0 / 256) % 256;
    rc4key[6] = ppk1 % 256;
    rc4key[7] = (ppk1 / 256) % 256;
    rc4key[8] = ppk2 % 256;
    rc4key[9] = (ppk2 / 256) % 256;
    rc4key[10] = ppk3 % 256;
    rc4key[11] = (ppk3 / 256) % 256;
    rc4key[12] = ppk4 % 256;
    rc4key[13] = (ppk4 / 256) % 256;
    rc4key[14] = ppk5 % 256;
    rc4key[15] = (ppk5 / 256) % 256;
     printf("first Time Elapsed : %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
   // end= clock();

Even the most optimistic precision (not accuracy) figures for clock() are at least 1M times slower than the clock frequency of most modern desktop machines.

Not that it stops you from using clock(). All you gotta do is just put the code in a loop and run it a few 100/1000 times. You'll have your figures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.