dimanche 28 juin 2015

Why is modulus with unsigned int runs faster than with unsigned long long?

Why to test speed of modulus?


I have an app where modulus operation is performed millions times a second. I have to work with very big numbers, so I chose unsigned long long as a data type. About a week ago I've written a new algorithm for my app that required performing modulus operation on numbers which are much less than the numbers I used to work with (e.g. 26 instead of 10000000). I chose to use unsigned int as a data type. The speed increased dramatically while the algorithm is almost the same.

Testing...


I've written two simple programs in C to test the speed of modulus calculation.

#include <stdio.h>

typedef unsigned long long ull;

int main(){
   puts("Testing modulus...");
   ull cnt;
   ull k;
   for(k=1, cnt=98765432;k<=10000000;++k) 
      printf("%u ",cnt%80);
   puts("");
   return 0;
}

The only thing I was changing was the type of the variable called cnt. I added the printf call to be sure that nothing gets optimized away.

I tested these programs with time ./progname > /dev/null and the results were as follows.

  • With unsigned long long: 14.5 sec
  • With unsigned int: 11.2 sec

Note: I'm testing it on a jailbroken iPad, that's why it takes so much time.

Why?


Why does the version with unsigned long long take so much time to run?

Multithreading project structure

I'm developing a project that involves the interaction between three threads. The code of the the thread functions is very long, and put everything in a file does not seem a good idea, because it becomes unmanageable.

Compile separately the threads functions and then link everything is a solution that makes sense?

Obviously there are a lot of data structures shared between threads.

What is the proper way to separate the project into more files?

Can anyone modify the following code to avoid the usage of "if"?

I am working on cuda currently and I got stuck on the code below. The code was originally written in matlab and I am trying to re-write it using cuda:

Pv = 0; Nv =0;
[LOOP]
v1 = l(li);
v2 = l(li+1);
if((v1>0) && (v2>0))
    Pv = Pv + 1;
elseif((v1<0) && (v2<0))
    Nv = Nv +1;
elseif((v1>0) && (v2<0))
    r = v1/(v1-v2);
    Pv = Pv + r;
    Nv = Nv + 1 - r;
elseif((v1<0) && (v2>0))
    r = v2/(v2-v1);
    Pv = Pv + r;
    Nv = Nv + 1 - r;
end
[LOOP END]

But, in cuda architecture, "if" expression is sometimes expensive, and I believe there is some way to avoid the usage of it, although I cannot figure it out now.

The main purpose of the code is to calculate the ratio of positive interval of negative interval and add them up respectively. In most of the situation , v1 and v2 have the same sign, but once they have different sign, I have to use a bunch of "if" or even perhaps "abs()" to handle the situation.

So, can anyone help me to re-write the code using C while using as few "if " as possible?

Is it necessary to close a file number of time it is opened in program?

If file is opened using fopen() and in different mode then is it necessary to close it number of time or it can be closed once at end of code ?

implement the char ' | ' to get a pipe between two processes in C [duplicate]

This question already has an answer here:

I have to implement the pipe char ' | ' in my own mini shell in C (Linux). for exemple.... after compiling file.c and then ./a.out I should be able to digit ls | sort, and the program should be able to work as the main shell. How can I do such a thing? thanks

Function to read sentence from user input [C]

I am trying to read sentence from user input problem with my function is it skips second try when I try to call it... Any solution?

void readString(char *array, char * prompt, int size) {
    printf("%s", prompt);
    char c; int count=0;
    char * send = array;
    while ((c = getchar()) != '\n') {
        send[count] = c; count++;
        if (size < count){ free(array); break; } //lets u reserve the last index for '\0'
        }

    }

Here is how try to call it:

char obligation[1500];
char dodatno[1500];

readString(obligation, "Enter obligation", 1500);
readString(dodatno, "Enter hours", 1500);

Here is example of inputs: "This is some sentence"

so latter I wana do this:

printf(" %s | %s \n",obligation, dodatno);

and get:

This is some sentence|This is another sentence

Finite State Machine In C

I am trying to create a simple finite state machine in C and I'm quite confused on how to get started. I tried looking online but nothing has really cleared this up for me.

My goal is to check if a string is octal, hex or an integer.

My attempt at creating the states would be:

typedef enum  {
   ERROR,
   OCTAL,
   HEX,
   INTEGER
} stringStates;

Now, I would then use a switch statement to go through the entirety of the string and switch between the different states until I have correctly identified which state it belongs to.

 while (current_Position<=end_String-1)
 {
    switch( "input something here")
      {
        case 0:
             //process string
             break;
        case 1:
             //process string
             break;

         case 2:
             //process string
             break;
         case 3:
             //process string
             break;
         default:
             break;
      }
  }

This concept is still very new to me and I'm having hard time understanding its implementation. If anyone can shed some light, it'll be much appreciated.