I tend to use CGFloat all over the place, but I wonder if I get a senseless "performance hit" with this. CGFloat seems to be something "heavier" than float, right? At which points should I use CGFloat, and what makes really the difference?
This question is related to
iphone
objective-c
cocoa-touch
CGFloat is a regular float on 32-bit systems and a double on 64-bit systems
typedef float CGFloat;// 32-bit
typedef double CGFloat;// 64-bit
So you won't get any performance penalty.
From the Foundation source code, in CoreGraphics' CGBase.h
:
/* Definition of `CGFLOAT_TYPE', `CGFLOAT_IS_DOUBLE', `CGFLOAT_MIN', and
`CGFLOAT_MAX'. */
#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif
/* Definition of the `CGFloat' type and `CGFLOAT_DEFINED'. */
typedef CGFLOAT_TYPE CGFloat;
#define CGFLOAT_DEFINED 1
Copyright (c) 2000-2011 Apple Inc.
This is essentially doing:
#if defined(__LP64__) && __LP64__
typedef double CGFloat;
#else
typedef float CGFloat;
#endif
Where __LP64__
indicates whether the current architecture* is 64-bit.
Note that 32-bit systems can still use the 64-bit double
, it just takes more processor time, so CoreGraphics does this for optimization purposes, not for compatibility. If you aren't concerned about performance but are concerned about accuracy, simply use double
.
In Swift, CGFloat
is a struct
wrapper around either Float
on 32-bit architectures or Double
on 64-bit ones (You can detect this at run- or compile-time with CGFloat.NativeType
) and cgFloat.native
.
From the CoreGraphics source code, in CGFloat.swift.gyb
:
public struct CGFloat {
#if arch(i386) || arch(arm)
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Float
#elseif arch(x86_64) || arch(arm64)
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Double
#endif
*Specifically, long
s and pointers, hence the LP
. See also: http://www.unix.org/version2/whatsnew/lp64_wp.html
As others have said, CGFloat is a float on 32-bit systems and a double on 64-bit systems. However, the decision to do that was inherited from OS X, where it was made based on the performance characteristics of early PowerPC CPUs. In other words, you should not think that float is for 32-bit CPUs and double is for 64-bit CPUs. (I believe, Apple's ARM processors were able to process doubles long before they went 64-bit.) The main performance hit of using doubles is that they use twice the memory and therefore might be slower if you are doing a lot of floating point operations.
just mention that - Jan, 2020 Xcode 11.3/iOS13
Swift 5
From the CoreGraphics source code
public struct CGFloat {
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Double
Source: Stackoverflow.com