Python, C++ and SWIG



Similar documents
Jonathan Worthington Scarborough Linux User Group

Using EDA Databases: Milkyway & OpenAccess

Chapter 3.2 C++, Java, and Scripting Languages. The major programming languages used in game development.

Source Code Translation

Enterprise Mobile Application Development: Native or Hybrid?

The Plan Today... System Calls and API's Basics of OS design Virtual Machines

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Creating Dynamics User Model Dynamic Linked Library (DLL) for Various PSS E Versions

The C Programming Language course syllabus associate level

Driving force. What future software needs. Potential research topics

General Introduction

1/20/2016 INTRODUCTION

PCI vs. PCI Express vs. AGP

Xcode Project Management Guide. (Legacy)

Object Oriented Database Management System for Decision Support System.

Mark Bennett. Search and the Virtual Machine

Adding scalability to legacy PHP web applications. Overview. Mario Valdez-Ramirez

Programming models for heterogeneous computing. Manuel Ujaldón Nvidia CUDA Fellow and A/Prof. Computer Architecture Department University of Malaga

C# and Other Languages

Techniques and Strategies for Data-driven design in Game Development. Scott Shumaker Outrage Games

Cloud Computing. Up until now

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Chapter 2: Remote Procedure Call (RPC)

Abstract. Description

A Comparison of Programming Languages for Graphical User Interface Programming

Python for Series 60 Platform

Chapter 5: Operating Systems Part 1

Effective Java Programming. efficient software development

Visual Basic Programming. An Introduction

Multi-core Programming System Overview

Base One's Rich Client Architecture

How to Choose the Right Data Storage Format for Your Measurement System

Here s how to choose the right mobile app for you.

SLANGTNG - SOFTWARE FOR STOCHASTIC STRUCTURAL ANALYSIS MADE EASY

WHITE PAPER. TimeScape.NET. Increasing development productivity with TimeScape, Microsoft.NET and web services TIMESCAPE ENTERPRISE SOLUTIONS

Building Applications Using Micro Focus COBOL

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Chapter 3. Database Environment - Objectives. Multi-user DBMS Architectures. Teleprocessing. File-Server

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Cover sheet. How do you create a backup of the OS systems during operation? SIMATIC PCS 7. FAQ November Service & Support. Answers for industry.

A TOOL FOR SUPPORTING THE PROCESS OF PROPERTY MANAGEMENT AND THE CREATION OF TECHNICAL DRAWINGS

WordPress Optimization

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Lesson 1 Introduction to Rapid Application Development using Visual Basic

Introduction (Apps and the Android platform)

OPC COMMUNICATION IN REAL TIME

Cambridge International AS and A Level Computer Science

System Architecture V3.2. Last Update: August 2015

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

A deeper look at Inline functions

Matrox Imaging White Paper

Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

Applications to Computational Financial and GPU Computing. May 16th. Dr. Daniel Egloff

Configuration Management: An Object-Based Method Barbara Dumas

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

NoSQL replacement for SQLite (for Beatstream) Antti-Jussi Kovalainen Seminar OHJ-1860: NoSQL databases

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Optimally Manage the Data Center Using Systems Management Tools from Cisco and Microsoft

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Fourth generation techniques (4GT)

SAP HANA - Main Memory Technology: A Challenge for Development of Business Applications. Jürgen Primsch, SAP AG July 2011

Writing standalone Qt & Python applications for Android

Reconfigurable Architecture Requirements for Co-Designed Virtual Machines

Component visualization methods for large legacy software in C/C++

Module I-7410 Advanced Linux FS-11 Part1: Virtualization with KVM

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

The Bus (PCI and PCI-Express)

Revolutionized DB2 Test Data Management

Optimizing Ext4 for Low Memory Environments

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

In-memory Tables Technology overview and solutions

Software Development for Embedded GNU Radio Applications

Lecture 1 Introduction to Android

Professional. SlickEdif. John Hurst IC..T...L. i 1 8 О 7» \ WILEY \ Wiley Publishing, Inc.

Glossary of Object Oriented Terms

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Object Instance Profiling

Getting Started with the Internet Communications Engine

Upping the game. Improving your software development process

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

System Structures. Services Interface Structure

sys socketcall: Network systems calls on Linux

Lua as a business logic language in high load application. Ilya Martynov ilya@iponweb.net CTO at IPONWEB

MAGENTO HOSTING Progressive Server Performance Improvements

GTask Developing asynchronous applications for multi-core efficiency

New Generation of Software Development

Bringing Big Data Modelling into the Hands of Domain Experts

Parallels Virtuozzo Containers vs. VMware Virtual Infrastructure:

Transcription:

Robin Dunn Software Craftsman O Reilly Open Source Convention July 21 25, 2008 Slides available at http://wxpython.org/oscon2008/

Python & C++ Comparisons Each is a general purpose programming language, applicable to all sorts of application domains client/server GUI/WUI database computational business games etc. But each language also has its own set of strengths and weaknesses

Python & C++ Comparisons C++ statically typed compiled complex close to the metal fast at runtime long edit/compile/run cycles typically lots of code needed to solve a problem plenty of ways to shoot yourself in the foot Python dynamically typed interpreted simple higher level slow, but usually fast enough quick edit/run cycles typically 5-10 times less code to solve same problem your foot is much more safe

Python & C++ Comparisons In the end it all boils down to Speed Since C++ is close to the metal the compiled code has little runtime overhead and can be very quick executing. For many situations however there are much slower bottlenecks than the CPU so the speed of C++ isn't as valuable as you might think for these tasks. Productivity Because of the simpler language, dynamic typing (plus duck typing ) and high level language support of advanced container and other types, much less code is needed and the programmer can be much more productive. Python has no need for pointers, templates, macro preprocessor, explicit memory allocation, etc. Common anecdotal rule: one Python programmer can do in 2 months what 2 C++ programmers are not able to finish in one year.

Best of Both? So is it possible to have the speed of C/C++ where it's needed, and the productivity, flexibility, ease of maintenance, and joy of using Python, all in the same application?

Best of Both? Yes! Python is designed from the beginning to be extensible in C The calling code doesn't need to know if the module is Python or C code Enables porting modules to C on an as-needed basis after prototyping and profiling, without disturbing the rest of the application Can be anything from simple wrappers around C library functions to very Python-esque things like new class types, etc.

Python C API Broad and comprehensive APIs Used by extension modules to provide the glue between the Python code and the C/C++ code. Can deal with Python objects at an abstract level (e.g. anything that can act like a number) Or at a concrete level, (e.g. when you know you have an integer object) Can also raise Python exceptions call Python functions/methods manipulate lists, dictionaries, etc. And so on.

Extension modules Compiled and linked as a shared object file or DLL Placed on the Python path Used with the normal Python import statement Python uses the platform's normal APIs for dynamic loading Python calls a known initialization function, which creates the module object and all of its content You can then use the module the same as if it was written in Python instead of C.

Extension Modules You still have to deal with the negative aspects of C/C++, (complex code, memory allocations, longer edit/compile/run cycles, etc.) but: It is for a relatively small portion of the whole application, just where speed or time critical things matter Usually can be a self contained library Usually can be easily unit tested to help maintain reliability, because the unit testing can happen from the Python side Productivity gains of using Python for the rest of the application greatly outweigh extra work needed for making an extension module.

Basic Architecture Python Code for Application Python Bytecode Interpreter C/C++ Extension Module(s) Python C API OS and Hardware

What's the Downside? Writing large extension modules by hand can be mindnumbingly tedious and error-prone grunt work The process Write a Python wrapper function for every C function you want to access Convert input parameters Call target function Check for errors/exceptions Convert return value Create Python versions of C constants Provide access to C variables, structures, and C++ classes Write an initialization function Imagine repeating that for a huge library with thousands of functions Imagine propagating API changes in the target library by hand through the extension module code and the Python wrapper code

SWIG Simplified Wrapper Interface Generator Automates a huge amount of the work for extension modules, eliminating most of the time and tedium for development of extensions Facilitates the gluing of C/C++ libs to Python, allowing you to partition the application to the languages that can best handle each kind of task, building on strengths instead of living with weaknesses

SWIG A compiler that reads C/C++ declarations and writes C code implementing the extension Support for wrapping C structures and C++ classes, in addition to standalone functions, constants, etc. Also supports extensions for languages other than Python Understands the C++ class hierarchy and can do appropriate type checking of input parameters, etc. Can generate code for use with C++ templates Can generate code that allows overriding C++ virtual methods in Python classes

SWIG Many things work out of the box with no extra work, but SWIG also provides many ways to customize or add to the generated code %typemap: controls what C code is generated in wrapper functions that receive or return objects of certain types %extend: Can add new methods to a wrapped class that don't necessarily exist in the C++ class %inline: Add new C functions that will automatically be wrapped %pythoncode: Emit custom python code into the wrapper for the current class, or append or prepend it to the Python proxy function Etc.

SWIG Includes a full C/C++ preprocessor, with some SWIG extensions for richer macros, etc. Can use macros and conditional compilation directives in the interface declaration files SWIG can understand the real headers for the library, and as long as they are clean it can sometimes make sense to use them directly with no extra work needed.

SWIG SWIG can either generate a bare extension module full of wrapped functions Or a combination of an extension module and a Python module with Proxy classes. This is the best option for wrapping C++ classes by providing a natural mapping to Python classes Offers lots of options for customizing or tweaking the API by emitting additional Python code Can also easily create a class-based API from wrappers of plain C functions The only attribute in the Python proxy is an object that contains a pointer to the real C++ object

SWIG themodule.i SWIG themodule.py _themodule.so themodule_wrap.cpp thelibrary C/C++

Why to not use SWIG Code generated by SWIG is not the best that an extension module can be. Classes are flattened to a collection of wrapped functions, with the OO-ness added back on by the Proxy classes. A hand-coded extension module would probably just use extension types (classes) instead The flexibility and generality of SWG generated code leads to some inefficiencies that could be avoided with hand-coding Like probably any code generator, the code is messy and not much fun to read, but luckily you seldom need to do so

Why use SWIG SWIG saves massive amounts of time and effort wxpython is 1.6 MLOC, 90-95% generated by SWIG, and not only maintained, but actively developed by one person Allows developers to partition the application into the languages best suited to performing each task, without needing to spend relatively large amounts of time on the glue code Developers can spend more time on the parts of the code that really matter