Posted in Programming

Link for tutorials

http://www.javatpoint.com/cpp-tutorial

Advertisements
Posted in Programming

Resource Usage when allocating RAM

[root@serv-1 dev]# g++ getrusage.c -o get_resource_usage
[root@serv-1 dev]# time ./get_resource_usage
RAM size[MB]: 12
        User CPU: 0.005881
        System CPU: 0.011762
RAM size[MB]: 22
        User CPU: 0.008497
        System CPU: 0.01605
RAM size[MB]: 32
        User CPU: 0.008659
        System CPU: 0.022129
RAM size[MB]: 42
        User CPU: 0.010456
        System CPU: 0.026617
RAM size[MB]: 52
        User CPU: 0.011487
        System CPU: 0.032548
RAM size[MB]: 62
        User CPU: 0.013506
        System CPU: 0.035694
RAM size[MB]: 72
        User CPU: 0.013506
        System CPU: 0.039341
RAM size[MB]: 82
        User CPU: 0.014366
        System CPU: 0.041182
RAM size[MB]: 92
        User CPU: 0.014366
        System CPU: 0.043871
RAM size[MB]: 102
        User CPU: 0.015425
        System CPU: 0.045312

real    0m0.069s
user    0m0.015s
sys     0m0.051s
[root@serv-1 dev]# cat getrusage.c
#include <sys/resource.h>
#include <sys/time.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

#define TO_SEC 1000000

int main() {
  int i = 0;
  struct rusage r_usage;
  struct timeval t_utime, t_stime ;
  while (++i <= 10) {
    void *m = malloc(10*1024*1024);
    memset(m,0,10*1024*1024);
//    usleep(2000000);
    getrusage(RUSAGE_SELF,&r_usage);
    std::cout << "RAM size[MB]: " << r_usage.ru_maxrss/1024 << std::endl ;
    t_utime = r_usage.ru_utime ;
    t_stime = r_usage.ru_stime ;
    std::cout << "\tUser CPU: "
        << (float) t_utime.tv_usec/TO_SEC
        << "\n"
        << "\tSystem CPU: "
        << (float) t_stime.tv_usec/TO_SEC
        << std::endl ;
  }
  return 0;
}

Posted in 2016, Programming

Learning C++ – Linked List – addNextItem

Youtube is full of “Everyone needs to know how to code” so I try to learn some basics in c++, but it’s in the beginning, I hope I will make use of it in my Raspberry Pi MiniProjects

struct node {
int data ;
node *next ;
} ;

void linkedListDisplay(node *start){
    int i = 0 ;
    while (start!=NULL){
        i++ ;
        cout << i << ":" << start->data << endl ;
        start = start->next ;
    }
}

node *initLinkedList(){
    node *ptr = new node ;
    ptr->data = 10 ;
    ptr->next = NULL ;

    return ptr;
}

node *addNextItem(node *previous_addr, int &dataInput){
    node *ptr = previous_addr ;

    node *newItem = new node ;
    newItem->data = dataInput ;
    newItem->next = NULL ;
    ptr->next = newItem ;

    return newItem ;
}

int main()
{
    node *head = initLinkedList() ;
    node *t = head ;
    node *next ;

    int value ;
    cout << "Please input a number: " ;
    cin >> value ;
    next=addNextItem(t, value) ;
    t=next ;
    
    cout << "Please input a number: " ;
    cin >> value ;
    next=addNextItem(t, value) ;
    t=next ;

    linkedListDisplay(head) ;

    return 0;
}