Code Efficiency Improvement

10 downloads 26722 Views 469KB Size Report
Code efficiency, introductory programming courses, mobile software, mobile applications. 1. INTRODUCTION. We have been witnessing the “mobile revolution” ...

Proceedings Emerging Trends in Advanced Computing & Information Communication Technologies (ETACICT-2014)

Incorporation of Guidelines for Code Efficiency Improvements in Introductory Programming Courses S.R. Subramanya School of Engineering, Technology, and Media National University San Diego, CA 92123, USA

[email protected] ABSTRACT Recent trends in mobile computing are leading to increased deployment of several applications – both consumer and enterprise – on mobile devices (phones and tablets). The mobile devices are severely battery power constrained. It is crucial for the code running on mobile devices to be extremely efficient in terms of not draining battery power. In this paper, we present several guidelines for improving the efficiencies of the source code targeted for mobile devices. We also make a case for incorporating good coding practices targeted for mobile devices in introductory programming courses. Keywords Code efficiency, introductory programming courses, mobile software, mobile applications

1. INTRODUCTION We have been witnessing the “mobile revolution” leading to the ubiquity of mobile devices (mobile phones and tablets). In addition to communications, the mobile phones are also used considerably for data applications. There are increasing demands for developing newer applications for mobile devices, as well as porting existing desktop and server-based applications. Although the processor power and memory capacity have been increasing in mobile devices, so has the complexity and size of the code (both the operating system and the applications) using up the increased processing power and memory. This has put tremendous strain on the battery power, since it has not kept pace with the increases in processor power, memory capacity, and software complexity. Thus, there is a dire need for the code in mobile devices to be elegant and efficient in order to conserve battery power. Although compilers perform several code optimizations, they do not cover all possible instances. Thus it is important for the programmers and software engineers to develop a mindset of writing efficient programs. Most introductory programming courses commonly use C++ or Java languages. In many cases, it is a sequence of two courses – the first part introducing the syntax and basic concepts such as the elementary data types, control structures, functions, and the second part covering advanced data structures, object-oriented concepts, etc. There are numerous books on these topics [5, 6, 7, 8, 10, 11]. However, writing efficient code has not been sufficiently addressed as part of introductory programming courses, and in the typical textbooks. In this paper we present several representative guidelines for improving the code efficiency. Readability of the code is equally important considering the fact that most effort is spent in code maintenance (bug fixes, functionality enhancements, performance improvements, porting to different hardware, etc.). Therefore, these efficiency enhancing coding guidelines should not make the code too obfuscated and unreadable. It should be

noted the guidelines presented in this paper cover several typical cases, and by no means exhaustive (due to space limitations). Considering the facts that: (a) considerable programming is targeted for battery–power constrained mobile devices, (b) it is important to employ practices for writing efficient code during programming, (c) it is crucial to develop the right mindset toward code efficiency in the early stages of learning programming, we believe that incorporating guidelines for improved code efficiency in the introductory programming courses is important. In the next section we present background and motivation for the incorporation of guidelines for efficient coding practices in introductory programming courses. Section 3 presents several proposed guidelines for efficient coding. We also provide the results of code improvements for a couple of cases in Section 4, followed by conclusions.

2. BACKGROUND AND MOTIVATION In this section, we present a few samples of work related to improving the efficiency of the source code. We also give the motivation for paying attention to efficiency aspects while programming and make a case for the inclusion of related guidelines in introductory programming courses. The advances in battery power/capacity have not kept pace with the other aspects of mobile devices such as, (a) the advances in processor power, (b) memory capacity, and (c) complexity of the software being run. Energy density rises at a yearly rate of around 5%, making the growth of battery capacity/power slower compared with the processing power that doubles almost every 18 months, according to Global Industry Analysts [1]. Since battery power has not kept up with the processing power and memory capacity, it continues to be a constrained resource in mobile devices [9]. A source code transformation technique based on value profiling for energy-efficient system design is presented in [4], which reduces the computational effort of the program by specializing it for highly expected situations. The processor running the optimized program can be shut down or slowed down for energy saving without performance degradation. Experimental results have shown energy efficiency improvements of more than two times over the original code. Compilation techniques which produce efficient code with low energy requirements is given in [12]. Despite the modern optimizing compilers producing good assembly / machine code, writing efficient programs, has advantages. For example, the optimizing compilers may not produce the most efficient code in all cases. Also, during debugging (using JTAG, for example), the corresponding assembly code is traced. Generally a well written efficient code has a better correspondence with the associated assembly code and therefore easy to locate the error and maintain code.

Galgotias College of Engineering & Technology - 1, Knowledge Park-II, Greater Noida, 201306

Proceedings Emerging Trends in Advanced Computing & Information Communication Technologies (ETACICT-2014) [13] presents the case that increases in processing power and A B C Condition memory capacity have resulted in software where efficiency 0 0 0 0 issues have taken a back seat, resulting in software which has 0 0 1 0 more ‘girth’ than is necessary for its functionality. Further, the 0 1 0 0 way to streamline software lies in disciplined methodologies and 0 1 1 1 a return to the essentials. It presents a description of Project 1 0 0 0 Oberon, which has been in use since 1989, whose primary goal 1 0 1 1 was to show that software can be developed with a fraction of 1 1 0 1 the memory capacity and processor power usually required 1 1 1 1 without sacrificing flexibility, functionality, or user convenience. With the mobile phones and tablets being increasingly and ubiquitously used for numerous daily tasks such as email, messaging, Web browsing, maps and navigation, running a variety of mobile Apps, etc., the users deem it inconvenient to charge the mobile devices too often. Improving the efficiency of the source code of the software in mobile devices is one among several things needed to enhance the efficiency of batter power usage of mobile devices. It is thus important for the new breed of programmers entering the software development arena either as individuals, or employees of small and medium companies, or large corporations to develop a mindset for efficient programming. The traditional introductory programming courses do not sufficiently address the aspects of efficient coding. There have been a few books on writing efficient programs. For example, fundamental rules, space-for-time rules, time-for-space rules, loop rules, logic rules, procedure rules, expression rules, and system-dependent optimization are presented in [2]. Numerous insights and techniques of improving efficiencies of programs are given in [3]. However, these are not used in mainstream introductory programming courses. Only the motivated and passionate programmers pursue the efficiency enhancing techniques on their own, or programmers are mentored in some companies by experienced lead programmers. However, it is our view that every programmer writing new code or porting code targeted for mobile devices should actively (and passionately) pursue techniques of writing highly efficient code.

3. PROPOSED GUIDELINES In this section, we present several guidelines for improving the efficiency of source code. The guidelines are programming language agnostic and can be adapted to the current, commonly used languages such as C, C++, and Java. The original code segments have been gathered from source code in some commercial systems. They have been stripped of specific details and adapted to retain the essence. We provide justifications that the code transformations are correctness– preserving for the appropriate cases. It should be noted that the proposed guidelines are representative of several typical cases, and by no means exhaustive (due to space limitations). The objective is to make a case for the importance of including code efficiency guidelines while teaching introductory programming, due to the fact that considerable code is targeted for battery power constrained mobile devices. In the following subsections we present several representative guidelines.

3.1 Condition check simplification Consider the following condition (adapted from source code of a commercial software): ( (A > 0 && (B > 0 || C > 0)) || (B > 0 && (A > 0 || C > 0)) || (C > 0 && (A > 0 || B > 0)) ) The corresponding truth table for the condition is given below.

The corresponding Karnaugh map is given below:

After Boolean simplification using the Karnaugh map, the simplified condition would be: AB + BC + AC = (A && B) || (B && C) || (A && C) Note that the transformed condition is much simpler and easier to comprehend. There are 17 comparisons in the original version versus 5 in the modified version.

3.2 Condition elimination Several times, ‘if – else-if’ condition statements are used to test a range of values of a variable and a resulting value is returned depending on the interval where the value of the variable falls. In the example below, the valid values of the variable ‘curDay’ can only be in the range 1 – 31. The function checks if the value of ‘curDay’ falls in any of the 5 intervals, and correspondingly returns values 1, 2, … 5. Value of currentDay Return value of the function


8 – 14

15 – 21

22 – 28

> 28






Consider the original code shown below: byte week_num (byte curDay){ if(curDay - DAYS_IN_WEEK < 1) return 1; else if(curDay-2*DAYS_IN_WEEK < 1) return 2; else if(curDay-3*DAYS_IN_WEEK < 1) return 3; else if(curDay-4*DAYS_IN_WEEK < 1) return 4; else return 5; } Multiplications are expensive operations. This can be simplified by avoiding the multiplications, as shown in the modified code below. byte week_num (byte curDay){ if(curDay

Suggest Documents