2018年2月1日 星期四

C qsort() vs C++ sort()


Source: https://www.geeksforgeeks.org/c-qsort-vs-c-sort/


Standard C library provides qsort function that can be used for sorting an array. Following is the prototype of qsort() function.
// Sort an array of any type. The parameters are, base
// address of array, size of array and pointer to
// comparator function
void qsort (void* base, size_t num, size_t size, 
            int (*comparator)(const void*, const void*));
It requires a pointer to the array, the number of elements in the array, the size of each element and a comparator function. We have discussed qsort comparator in detail here.
C++ Standard Library provides a similar function sort() that originated in the STL. We have discussed C++ sort here. Following are prototypes of C++ sort() function.
// To sort in default or ascending order.
template 
void sort(T first, T last);

// To sort according to the order specified
// by comp.
template
void sort(T first, T last, Compare comp);
The order of equal elements is not guaranteed to be preserved. C++ provides std::stable_sort that can be used to preserve order.
Comparison to qsort and sort()
1. Implementation details:
As the name suggests, qsort function uses QuickSort algorithm to sort the given array, although the C standard does not require it to implement quicksort.
C++ sort function uses introsort which is a hybrid algorithm. Different implementations use different algorithms. The GNU Standard C++ library, for example, uses a 3-part hybrid sorting algorithm: introsort is performed first (introsort itself being a hybrid of quicksort and heap sort) followed by an insertion sort on the result.
2. Complexity :
The C standard doesn’t talk about its complexity of qsort. The new C++11 standard requires that the complexity of sort to be O(Nlog(N)) in the worst case. Previous versions of C++ such as C++03 allow possible worst case scenario of O(N^2). Only average complexity was required to be O(N log N).
3. Running time:
STL’s sort ran faster than C’s qsort, because C++’s templates generate optimized code for a particular data type and a particular comparison function.
STL’s sort runs 20% to 50% faster than the hand-coded quicksort and 250% to 1000% faster than the C qsort library function. C might be the fastest language but qsort is very slow.
When we tried to sort one million integers on C++14, Time taken by C qsort() was 0.247883 sec and time taken by C++ sort() was only 0.086125 sec
// C++ program to demonstrate performance of
// C qsort and C++ sort() algorithm
#include
using namespace std;
// Number of elements to be sorted
#define N 1000000
// A comparator function used by qsort
int compare(const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}
// Driver program to test above functions
int main()
{
    int arr[N], dupArr[N];
    // seed for random input
    srand(time(NULL));
    // to measure time taken by qsort and sort
    clock_t begin, end;
    double time_spent;
    // generate random input
    for (int i = 0; i < N; i++)
        dupArr[i] = arr[i] = rand()%100000;
    begin = clock();
    qsort(arr, N, sizeof(int), compare);
    end = clock();
    // calculate time taken by C qsort function
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    cout << "Time taken by C qsort() - "
         << time_spent << endl;
    time_spent = 0.0;
    begin = clock();
    sort(dupArr, dupArr + N);
    end = clock();
    // calculate time taken by C++ sort
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    cout << "Time taken by C++ sort() - "
         << time_spent << endl;
    return 0;
}
Output :
Time taken by C qsort() - 0.247883
Time taken by C++ sort() - 0.086125 
C++ sort() is blazingly faster than qsort() on equivalent data due to inlining. sort() on a container of integers will be compiled to use std::less::operator() by default, which will be inlined and sort() will be comparing the integers directly. On the other hand, qsort() will be making an indirect call through a function pointer for every comparison which compilers fails to optimize.
4. Flexibility:
STL’s sort works for all data types and for different data containers like C arrays, C++ vectors, C++ deques, etc and other containers that can be written by the user. This kind of flexibility is rather difficult to achieve in C.
5. Safety:
Compared to qsort, the templated sort is more type-safe since it does not require access to data items through unsafe void pointers, as qsort does.
References:
http://theory.stanford.edu/~amitp/rants/c++-vs-chttps://en.wikipedia.org/wiki/Sort_(C%2B%2B)
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice


2018年1月29日 星期一

Windows 7远程桌面连接Ubuntu 14.04

Windows 7远程桌面连接Ubuntu 14.04

原创 2016年02月13日 11:59:31

首先了解一下目前用于远程桌面的软件及所用的协议。
远程桌面连接组件是基于RDP(RemoteDesktopProtocol远程桌面协议)进行通信的。远程桌面协议(RDP, Remote Desktop Protocol)是一个多通道(multi-channel)的协议,让本地电脑连上提供微软终端机服务的电脑。大部分的Windows都有客户端所需软件。其他操作系统也有这些客户端软件,例如Linux、FreeBSD、Mac OS X。服务端电脑方面,则听取送到TCP3389端口的数据。
VNC(Virtual Network Computing)是基于RFB(Remote Frame Buffer)协议进行通信的,是一个基于平台无关的简单显示协议的超级瘦客户系统,由Cambridge的AT&T实验室设计开发的。vnc的缺省端口是main:5900(C/S)和http:5800(B/S)端口。VNC (Virtual Network Computer)是虚拟网络计算机的缩写。VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的。有很多著名的远程桌面软件都市基于这个vnc软件进行修改与包装的,如:Teamviewer、协通XT800、KDT。
通过上面的描述,我们在window上面使用基于RDP的viewer端,要想使用window的远程桌面,需要在 Ubuntu 上面运行一个基于rdp的server端。那么问题就是在Ubuntu上安装一个基于rdp的server端。XRDP 服务器就可以提供这样的服务。所以只要安装一个xrdp就可以了。
ubuntu14.04机器:
(1)
[python] view plain copy
  1. sudo apt-get install xrdp  
(2)
[python] view plain copy
  1. sudo apt-get install vnc4server  
(3)
[python] view plain copy
  1. sudo apt-get install xubuntu-desktop  
处理完后继续:
[python] view plain copy
  1. echo "xfce4-session" >~/.xsession  
最后重启:
[python] view plain copy
  1. sudo service xrdp restart  

另外在ubuntu机器上也要做如下的设置:


window7机器:

(1)使用"窗口键+R"打开"运行对话框"-->输入"mstsc"-->回车-->输入Ubuntu主机的IP地址-->"连接"。

填上正确的IP地址回车,会出现一个登陆框,我们选择“sessman-xvnc”这个,然后输入你的Ubuntu的用户名和密码,OK!


效果图如下:

2018年1月24日 星期三

[BUILD] iperf

## Build iperf-2.0.5 for MDM9607 LE 2.0.1 r00075

cd /work/yulin
git clone git://git.openembedded.org/meta-openembedded
cd meta-openembedded
git checkout -b iperf2 8a0667b25dfd25cae4ac200dcfb63d123858ad57

cd /work/yulin/ais9628/apps_proc/poky/meta-qti-bsp/recipes-extended/
rm -fr iperf
cp -pR /work/yulin/meta-openembedded/meta-oe/recipes-benchmark/iperf .

cd /work/yulin/ais9628/apps_proc/poky/
. build/conf/set_bb_env.sh
export MACHINE=mdm9607
export DISTRO=mdm-perf
bitbake iperf

---> tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/iperf/2.0.5-r0/package/usr/bin/iperf

2018年1月18日 星期四

GRUB

GRUB2中文指南第二版(上)
https://wiki.ubuntu-tw.org/index.php?title=GRUB2中文指南第二版(上)

 https://www.howtogeek.com/196655/how-to-configure-the-grub2-boot-loaders-settings/

2018年1月17日 星期三

2018年1月15日 星期一

Ubuntu 14.04 setup


# Fail to boot from one partition
(ref: http://www.netadmin.com.tw/article_content.aspx?sn=1501070001&jump=3)

sudo update-grub
sudo apt-get install efibootmgr
sudo efibootmgr

# Install and enable SSH
sudo apt-get install openssh-server

# Setup environment
.bashrc
.ssh
.vimrc
.flexlmrc

# Install BeyondCompare
sudo dpkg -i bcompare-4.1.3.20814_amd64.deb

# Fix PuTTY can't show all 'ls -l' output.
# Fix vi keymap.
sudo nano /etc/default/locale


# Normalize disk labels.
ls -l /dev/disk/by-uuid
sudo gtk-launch /etc/fstab &
sudo mkdir /HOUSE
sudo mkdir /work

sudo mount -a
sudo rmdir /opt
sudo ln -s /HOUSE/opt /opt


# Enable Samba server.
sudo apt-get install samba samba-common python-glade2 system-config-samba
sudo bcompare /HOUSE/etc/samba/smb.conf /etc/samba/smb.conf &
sudo service smbd restart
sudo smbpasswd -a

# Install screen utility
sudo apt-get install screen

# Use BASH
sudo ln -sf /bin/bash /bin/sh

# Update toolchain
sudo apt-get install build-essential chrpath coreutils desktop-file-utils diffstat docbook-utils fakeroot g++ gawk gcc git git-core help2man libgmp3-dev libmpfr-dev libreadline6-dev libtool libxml2-dev make python-pip python-pysqlite2 quilt sed subversion texi2html texinfo unzip wget

# Support 32-bits tools. Fix files are there and executable but not when invoked.
sudo echo "foreign-architecture i386" > /etc/dpkg/dpkg.cfg.d/multiarch
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386

# Install dmverity
sudo apt-get install cryptsetup-bin

# Install xmlstartlet
sudo apt-get install xmlstartlet

# Unify tab size to 4
$ tabs -4
$ git config --global core.pager 'less -x1,5'
$ grep tabstop ~/.vimrc
set tabstop=4

2018年1月13日 星期六

Ubuntu 16.04 setup

# Fail to boot from one partition
sudo update-grub
sudo efibootmgr

# Install and enable SSH
sudo apt-get install openssh-server

# Use BASH 
sudo ln -sf /bin/bash /bin/sh

# Setup environment
.bashrc
.ssh
.vimrc 
.flexlmrc

# Setup NFS client
sudo apt-get update
sudo apt-get install nfs-common

# Setup Samba server
sudo smbpasswd -a <username>

# Normalize disk labels. (/work /HOUSE /opt/<Platform>
add "172.16.245.9:/LinuxFile/t5r021 /HOUSE nfs rsize=20480,wsize=20480,exec 0 0" in /etc/fstab
sudo mount -a

ls -l /dev/disk/by-uuid
sudo gtk-launch /etc/fstab & 
sudo mkdir /HOUSE 
sudo mkdir /work

# Fix PuTTU can't show all 'ls -l' output.
sudo nano /etc/default/locale

# Install screen utility
sudo apt-get install screen

# Install BeyondCompare
sudo dpkg -i /HOUSE/yulin/Installs/bcompare-4.1.3.20814_amd64.deb

# Install gitk
sudo apt-get install gitk

# Install dmverity
sudo apt-get install cryptsetup-bin

# Install links (lite HTTPS client)
sudo apt-get install links

# Install html2text
sudo apt-get install html2text

# Install xmlstartlet
sudo apt-get update
sudo apt-get install xmlstarlet

# Unify tab size to 4
$ tabs -4
$ git config --global core.pager 'less -x1,5'
$ grep tabstop ~/.vimrc
set tabstop=4

===== Specific for SA515M ================================================

# Edit the file to enable the universe and multiverse sources and disable the
Ubuntu installation CD source
sudo vi /etc/apt/sources.list
sudo apt-get update                #Update package list.
sudo apt-get upgrade              #Upgrade package list.

# Update toolchain
sudo apt-get install build-essential chrpath coreutils desktop-file-utils diffstat docbook-utils fakeroot g++ gawk gcc git git-core help2man libgmp3-dev libmpfr-dev libreadline6-dev libtool libxml2-dev make python-pip python-pysqlite2 quilt sed subversion texi2html texinfo unzip wget

sudo apt-get install desktop-file-utils diffstat docbook-utils fakeroot g++ gawk gcc git git-core help2man libgmp3-dev libmpfr-dev libreadline6-dev libtool libxml2-dev make python-pip python-pysqlite2 quilt sed subversion texi2html texinfo unzip wget  

sudo apt-get install libz-dev
#/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.02_linux/bin/arm-linux-gnueabihf-ld: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
sudo apt-get install zlib1g:i386


# Support 32-bits tools. Fix files are there and executable but not when invoked.
#sudo echo "foreign-architecture i386" > /etc/dpkg/dpkg.cfg.d/multiarch  

sudo apt-get update 

sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386

sudo apt-get install package-name:i386  

# Download Hexagon LLVM v8.3.10 and install