Re: "Ravenscar-like" profile for C/C++

From: Ioannis Vranos (ivr_at_guesswh.at.emails.ru)
Date: 04/25/04


Date: Sun, 25 Apr 2004 18:13:30 +0300


"Marc Le Roy" <invalide@invalide.com> wrote in message
news:c6gdub$j92$1@news-reader4.wanadoo.fr...
> Hello,
>
> ADA Ravenscar is a restricted subset of the ADA language that has been
> defined for real-time software development in safety critical
applications.
> Completed with additional restrictions like the ones defined in the SPARK
> profile, it allow to build very deterministic applications that support
> automatic static code analysis and schedulability analysis.
>
http://www.acm.org/pubs/articles/proceedings/ada/289524/p1-dobbing/p1-dobbing.pdf
>
> I would like to know if there is a similar standard for C / C++. I found
> only MISRA-C and EC++, but they are rather permissive with respect to the
> Ravenscar ADA profile. Moreover, because the ADA standard covers concepts
> that are out of the scope of the C/C++ standards, I suppose that an
> equivalent of the Ravenscar profile in C/C++ should make reference to an
> RTOS.

There is no reason for such a subset in C++. Use the part of C++ that fits
your needs. The whole language is designed for maximum run-time/space
efficiency. I place here the contents of a page of my old web site which i
think you will find useful:

--------------------------------------------------------

FAQ for common myths

Here ANSI/ISO C++ 1998 is discussed.

C++ is a general purpose programming language with a bias towards systems
programming. It is suitable for writing mission critical applications under
severe time and space constraints, like Operating Systems, real-time
simulations, etc. It is suitable for writing applications towards all kinds
of systems, from small embedded devices to large mainframes.

C++ is the dominant general purpose programming language. Other programming
languages like Visual Basic, C#, Java, etc are far behind in worldwide
adoption (a fact that many may not know due to commercial fads). In this FAQ
I shall be trying to debunk various C++ myths that are widespread and come
to my attention.

Myth 1

"C-style operations with low level stuff (built in arrays, etc) are faster
than high level C++ operations with containers and algorithms of the
standard library (templates, etc)".

Answer: This is a myth. Template and other constructs of the C++ standard
library are written in such a way so as to be as fast as possible and as
small as possible so as to be inlined. That means we usually have better
performance than our low level code, which is separated in functions and is
called with the cost of time that function calls have.

An example. Consider the code:

#include <ctime>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <valarray>
#include <algorithm>
#include <iostream>

// For qsort()
inline int cmp(const void *p1, const void *p2)
{
    return *static_cast<const int *>(p1)-*static_cast<const int *>(p2);
}

int main()
{
    using namespace std;

    const unsigned long SIZE=20*1024*1024;

    int *p1=new int[SIZE];

    srand(time(0));

    // Random numbers created in the array on the free store
    for(unsigned long i=0; i<SIZE; i++)
        p1[i]=rand();

    int *p2=new int[SIZE];

    memcpy(p2, p1, SIZE*sizeof(*p1));

    // The numbers are copied from p to a vector<int>
    vector<int>vecarray(p1, &p1[SIZE]);

    // The numbers are also copied to a valarray<int>.
    // valarray is an array container optimised for numerical computations.
    valarray<int>vlarray(p1, SIZE);

    clock_t t1, t2, t3, t4, t5, t6, t7, t8, temp;

    // Wait to begin from a clear tick for accurate results
    for(temp=t1=clock(); !(t1-temp); t1=clock())
        ;

    sort(vecarray.begin(), vecarray.end());

    t2=clock();

    // Wait to begin from a clear tick for accurate results
    for(temp=t3=clock(); !(t3-temp); t3=clock())
        ;

    qsort(p1, SIZE, sizeof(*p1), cmp);

    t4=clock();

    // Wait to begin from a clear tick for accurate results
    for(temp=t5=clock(); !(t5-temp); t5=clock())
        ;

    sort(p2, p2+SIZE);

    t6=clock();

    // Wait to begin from a clear tick for accurate results
    for(temp=t7=clock(); !(t7-temp); t7=clock())
        ;

    sort(&vlarray[0], &vlarray[0]+vlarray.size());

    t8=clock();

    delete[] p1;
    delete[] p2;

    cout<<"\nvector<int>/sort() sorting time: "<<t2-t1<<" translated in
seconds: ";
    cout<<static_cast<double>(t2-t1)/CLOCKS_PER_SEC<<endl;

    cout<<"\nvalarray<int>/sort() sorting time: "<<t8-t7<<" translated in
seconds: ";
    cout<<static_cast<double>(t8-t7)/CLOCKS_PER_SEC<<endl;

    cout<<"\nint */qsort() sorting time: "<<t4-t3<<" translated in seconds:
";
    cout<<static_cast<double>(t4-t3)/CLOCKS_PER_SEC<<endl;

    cout<<"\nint */sort() sorting time: "<<t6-t5<<" translated in seconds:
";
    cout<<static_cast<double>(t6-t5)/CLOCKS_PER_SEC<<endl;

}

This code creates a low level int array on the free store containing
"random" values, a second one with the same values, a C++ high level
vector<int> container and a C++ high level valarray<int> container with the
same contents. Then it measures the time each one needs to be sorted. At
the first C-style array the C subset qsort() is applied. At the second
C-style array the C++ high level sort() (template) is applied. For the C++
high level vector<int> and valarray<int> (templates), the C++ high level
sort() (template) is applied. At the end the program outputs the timings.

The results on a PIII 1000 MHz, 1024 MB RAM:

MINGW GCC version 3.1 (Windows port) :

g++ temp.cpp -o
temp -pedantic-errors -Wall -fexpensive-optimizations -O3 -ffloat-store -mcp
u=pentiumpro

C:\c>temp

vector<int>/sort() sorting time: 10345 translated in seconds: 10.345

valarray<int>/sort() sorting time: 8051 translated in seconds: 8.051

int */qsort() sorting time: 14050 translated in seconds: 14.05

int */sort() sorting time: 8342 translated in seconds: 8.342

Borland C++ 5.5:

bcc32 -O2 -6 temp.cpp

C:\c>temp

vector<int>/sort() sorting time: 14561 translated in seconds: 14.561

valarray<int>/sort() sorting time: 14140 translated in seconds: 14.14

int */qsort() sorting time: 15963 translated in seconds: 15.963

int */sort() sorting time: 14311 translated in seconds: 14.311

Intel C++ 6:

icl /EHa /G6 /O3 /Qansi /Za /Qipo temp.cpp

C:\c>temp

vector<int>/sort() sorting time: 10094 translated in seconds: 10.094

valarray<int>/sort() sorting time: 8743 translated in seconds: 8.743

int */qsort() sorting time: 12929 translated in seconds: 12.929

int */sort() sorting time: 9053 translated in seconds: 9.053

Visual C++ 2002 .NET:

cl /EHa /Ox /G6 /Za /Wp64 temp.cpp

C:\c>temp

vector<int>/sort() sorting time: 8522 translated in seconds: 8.522

valarray<int>/sort() sorting time: 8352 translated in seconds: 8.352

int */qsort() sorting time: 13209 translated in seconds: 13.209

int */sort() sorting time: 8392 translated in seconds: 8.392

So by using C++ higher level constructs we maintain at least the efficiency
of C low level equivalents while enjoying the high level comfort and safety
C++ provides. And in many cases we even gain in time/space efficiency.

References: http://www.research.att.com/~bs/esc99.html

http://www23.brinkster.com/noicys/docs/ms-speaking-cpp.pdf 215 KB PDF file

[Last updated: 27 February 2003]

Copyright © 2001-2003 Ioannis Vranos
-------------------------------------------

Regards,

Ioannis Vranos



Relevant Pages

  • Re: "Ravenscar-like" profile for C/C++
    ... > ADA Ravenscar is a restricted subset of the ADA language that has been ... The whole language is designed for maximum run-time/space ... "C-style operations with low level stuff ... C-style array the C++ high level sort(template) is applied. ...
    (comp.lang.c)
  • Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > powerful and systems programming language, ... But Ada is used for system programming language as well. ... Ada can do more low level stuff then C++ - or have you ever seen 24 bit ...
    (comp.lang.ada)
  • Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > powerful and systems programming language, ... But Ada is used for system programming language as well. ... Ada can do more low level stuff then C++ - or have you ever seen 24 bit ...
    (comp.lang.cpp)
  • Re: PL/I, COBOL, Advantages, Equivalence, et al
    ... There is no perfect programming language. ... Because the original design group tended to be more from the academic and theoretical world than from the practical world the original ADA implementations were so pathetic that even after DoD mandated its use by Aerospace contractors nearly all received variances to use assembly, C, Fortran and yes PL/I to get the job done. ... the compilers produced unacceptable code. ...
    (comp.lang.pl1)
  • Re: How come Ada isnt more popular?
    ... I think that Ada *and* Haskell will make an interesting ... I wonder why one wouldn't just use Monads in most cases? ... a systems programming language. ... important reason not to ignore functional programming is [... ...
    (comp.lang.ada)